一、利用while方法解决

function trim(str) {
while (str[0] == ' ') {
str = str.slice(1);
}
while (str[str.length - 1] == " ") {
str = str.slice(0, str.length - 1);
}
return str;
}

二、利用正则方法检测开头和结尾的空格

   function qdkg(str) {
// return str.replace(/^\s+/,"").replace(/\s+$/,"");
if (String.prototype.trim) {
return str.trim();
} else {
return str.replace(/^\s+/, "").replace(/\s+$/, "");
}
}

三  检测原型prototype中有没有trim()方法,如果没有则更改prototype

if(!String.prototype.trim){
String.prototype.trim = function () {
return this.replace(/^\s+/, "").replace(/\s+$/, "");
}
}

最新文章

  1. NSTimer整理总结
  2. List<Object>转换为JSONArray二
  3. 解决Linux系统下程序找不到动态库的方法
  4. 读Java Concurrency in Practice. 第六章.
  5. Nuget~管理自己的包包~丢了的包包快速恢复
  6. USACO Section 2.2: Party Lamps
  7. iOS: 学习笔记实例, 用代码控制视图创建与切换
  8. Thread-safety with regular expressions in Java
  9. Hadoop2.7.3+Spark2.1.0 完全分布式环境 搭建全过程
  10. linux系统增加开机启动服务/应用
  11. iOS开发出错whose view is not in the window hierarchy!的解决
  12. 大厂们的 redis 集群方案
  13. input子系统 KeyPad-Touch上报数据格式与机制【转】
  14. linux服务器上简单命令
  15. liunx top命令详解
  16. SQL 查询存储过程
  17. 【python-opencv】图像直方图
  18. apache重写规则 rewrite
  19. Error: Target id is not valid ABIs: no ABIs 解决方法
  20. Imperva正则表达式的添加以及使用

热门文章

  1. js重写系统的弹框
  2. windbg 定位崩溃问题
  3. new_images
  4. Android system :led_class驱动
  5. heightchart配置详解
  6. 查看linux 内核版本信息
  7. centos中PATH环境变量查看和修改
  8. CodeForces - 631C (截取法)
  9. 简单理解JVM与static{}
  10. normalization 阅读笔记