我是靠谱客的博主 笨笨衬衫,这篇文章主要介绍c语言endswith用法,js中的indexOf以及startsWith和endsWith方法,现在分享给大家,希望可以做个参考。

js有indexOf方法,却没有像java同样的startsWith 或者 endsWith 这样的方法。javascript

在js里,统统能够用

indexOf()!=-1 来代替。indexOf()!=-1 和 indexOf()>0 获得的结果是不一样的。

好比下面的例子:(功能:但愿输入的值不包含空格)java

if(str.indexOf(' ') != -1 ){this

alert("不能含有空格");spa

}prototype

这个方法能够检查到 str 的开头,中间,结尾是否是空格。code

但若是用下面这个方法,却只能检测到 str 中间 和 结尾的空格,检测不到str开头的空格。ip

if(str.indexOf(' ')> 0){

alert("不能含有空格");string

}

由于indexOf 的结果是从0开始的, 而 > 0 就是说排除了第一个 !it

js中startWith和endWith的扩展:

io

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;

};

最后

以上就是笨笨衬衫最近收集整理的关于c语言endswith用法,js中的indexOf以及startsWith和endsWith方法的全部内容,更多相关c语言endswith用法,js中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部