js 判断字符串是否包含某字符串,String对象中查找子字符,indexOf

2017-02-09 15:55:14
var Cts = "bblText";
 
if(Cts.indexOf("Text") > 0 )
{
    alert('Cts中包含Text字符串');
}

indexOf用法: 

返回 String 对象内第一次出现子字符串的字符位置。 
   
   strObj.indexOf(subString[, startIndex]) 
   
   参数 
   strObj 
   
   必选项。String 对象或文字。 
   
   subString 
   
   必选项。要在 String 对象中查找的子字符串。 
   
   starIndex 
   
   可选项。该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。 
   
   说明 
   indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。 
   
   如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。 
   
   从左向右执行查找。否则,该方法与 lastIndexOf 相同。 
   
   示例 
   下面的示例说明了 indexOf 方法的用法。 
   
   function IndexDemo(str2){ 
    var str1 = "BABEBIBOBUBABEBIBOBU" 
    var s = str1.indexOf(str2); 
    return(s); 
   } 

对于JavaScript的indexOf忽略大小写 

JavaScript中indexOf函数方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。 

indexOf函数是从左向右执行查找。否则,该方法与 lastIndexOf 相同。 

下面的示例说明了indexOf函数方法的用法。

function IndexDemo(str2){
   var str1 = "BABEBIBOBUBABEBIBOBU"
   var s = str1.indexOf(str2);
   return(s);
}


  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

  • 2017-11-19 00:17:51

    Java如何获取Date的“昨天”与“明天”示例代码

    最近在做项目的时候用到Date和Calendar比较多,而且用到的方式也比较全,突然想到一个问题,Java如何获取Date的"昨天"与"明天",也就是前一天和后一天呢?思考后写出了方法,想着万一以后用到,就总结出来,也方便有需要的朋友们参考借鉴,下面来一起看看吧。

  • 2017-11-23 02:00:51

    js 分页插件twbs-pagination

    ​cdn地址 http://www.bootcdn.cn/twbs-pagination/ 官网地址 可以在cdn地址上面查看到