js中的indexOf以及startsWith和endsWith方法
来源:互联网 发布:港湾网络 高层 编辑:程序博客网 时间:2024/06/05 09:12
js有indexOf方法,却没有像java一样的startsWith 或者 endsWith 这样的方法。
比如下面的例子:(功能:希望输入的值不包含空格)
if(str.indexOf(' ') != -1 ){
alert("不能含有空格");
}
这个方法可以检查到 str 的开头,中间,结尾是不是空格。
但如果用下面这个方法,却只能检测到 str 中间 和 结尾的空格,检测不到str开头的空格。
if(str.indexOf(' ') > 0){
alert("不能含有空格");
}
因为indexOf 的结果是从0开始的, 而 > 0 就是说排除了第一个 !
js中startWith和endWith的扩展:
String.prototype.endWith = function(str){ if(str==null || str=="" || this.length == 0 ||str.length > this.length){ return false; } if(this.substring(this.length - str.length)){ return true; }else{ return false; } return true;}; String.prototype.startWith = function(str){ if(str == null || str== "" || this.length== 0 || str.length > this.length){ return false; } if(this.substr(0,str.length) == str){ return true; }else{ return false; } return true; };
0 0
- js中的indexOf以及startsWith和endsWith方法
- startsWith(),endsWith(),indexOf()的作用
- java endsWith()和startsWith方法的解释
- startswith和endswith函数
- python startswith和endswith
- 在javascript里 有关 indexOf 或者 startsWith 或者 endsWith 这样的方法
- javascript中 startsWith , endsWith 方法
- Python中的startswith和endswith函数使用实例
- Python的startswith和endswith
- StringUtils.startsWith 和StringUtils.endsWith
- Python的startswith和endswith
- Python的startswith和endswith
- Python的 startswith 和endswith
- java startsWith和endsWith详解
- js 自己实现 endsWith startsWith contains
- python中startswith与endswith方法
- java startsWith和endsWith的用法
- java startsWith和endsWith的用法
- P1001 谁拿了最多奖学金
- 生活感悟
- Android数据库Realm实践
- 希尔排序算法思想
- Mybatis JdbcType与Oracle、MySql数据类型对应列表
- js中的indexOf以及startsWith和endsWith方法
- OpenCL程序编程基本流程
- 指定git路径,拉取远程所有的分支到本地,脚本 2
- 5-47 打印选课学生名单 (25分)
- mysql server 下载地址
- JVM:如何分析线程堆栈
- NFC技术特征以及原理
- 编程经验
- Linux wget 解决下载错误