String的一些基本操作(startsWith、split)和JS中的match、search和replace方法

来源:互联网 发布:怎么在手机淘宝买东西 编辑:程序博客网 时间:2024/06/05 22:56
<span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; line-height: 17.6000003814697px;"><strong></strong></span><pre name="code" class="java"><span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; line-height: 17.6000003814697px;">1.JS中的match、search和replace方法</span>
</pre><pre name="code" class="javascript" style="font-size: 16px;"><span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; line-height: 17.6000003814697px;">/* search方法</span>
 * str.search(regexp) * 查找字符串str中是否有匹配regexp的子串,如果匹配则返回匹配子串的索引。否则返回-1。 */"hello123".search(/\d+/);    // 5"hello123".search(/\d{4}/);  // -1 /* match方法 * str.match(regexp) * 直接看例子,分别带g标识和不带g标识 */str = "For more information, see Chapter 3.4.5.1 chapter 4.5";re = /(chapter \d+(\.\d)*)/i; found = str.match(re); // 输出为["Chapter 3.4.5.1", "Chapter 3.4.5.1", ".1"],第二、第三个元素为捕获 str = "For more information, see Chapter 3.4.5.1 chapter 4.5";re = /(chapter \d+(\.\d)*)/gi; found = str.match(re); // 输出为["Chapter 3.4.5.1", "chapter 4.5"]// 最基础使用var name = "hey duke";name = name.replace("k", "d");console.log(name);  // "hey dude" // 稍高级用法(正则模式替换)var alphaStr = "hello123".replace(/\d+/gi, "");console.log(alphaStr);  // "hello"// 带反向引用var alphaSt2 = "Smith John".replace(/(\w+)\s*(\w+)/gi, "$2, $1");console.log(alphaStr2); // "John, Smith" // 较高级用法:第二个参数设为function,修改匹配子串var hadoop = "Had".replace("d", function(match) {   return match+"doop";});console.log(hadoop);  // "Hadoop" "yyyXXzzzz".replace(/(X*)(z{3})/,                   function (str, p1, p2, offset, s) {                       console.log(offset);                       console.log(s);                      return str + " - " + p1 + " , " + p2;                   }                );/*其中str是匹配子字符串,p1、p2是捕获,offset是匹配发生位置索引,s是源字符串,这里是"yyyXXzzzz"输出为:3yyyXXzzzz"yyyXXzzz - XX , zzzz"*/

2.String的startWith方法的使用!

今天发现了一个startsWith()方法的重载,startsWith(String str,int Index),当时上网去查了一下没有后边这个方法,于是自己做了一下测试,现在给出如下详细解释,startsWith(String str)就是检查形参参数是否与你要检查的字符串开头相同,而startsWith(Sring str,int Index)则是从你要比较的原字符串的指定下标开始和形参作比较,这里边呢str.startsWith(String str)和str.startsWith(string str,0)是没有区别的,下边就看代码吧!

String st1 = "dsadas";      System.out.println("st1:"+st1.startsWith("sa",1));      String st2 = "dsadas";      System.out.println("st1:"+st2.startsWith("sa"));      String st3 = "dsadas";      System.out.println("st2:"+st3.startsWith("ds"));      String st4 = "dsadas";      System.out.println("st4:"+st4.startsWith("ds",0));      String st5 = "dsadas";      System.out.println("st5:"+st5.startsWith("ds",1));输出结果为:st1:truest1:falsest2:truest4:truest5:false

3.String的split(String regex,int limit)方法小结
split(String regex, int limit)方法,头一个参数String regex表示字符串分割的模式,包括分隔符和正则表达式;但是第二个参数limit比较迷糊人,api中这样解释:limit 参数控制模式应用的次数,因此影响所得数组的长度。如果该限制 n 大于 0,则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后一项将包含所有超出最后匹配的定界符的输入。如果 n 为非正,那么模式将被应用尽可能多的次数,而且数组可以是任何长度。如果 n 为 0,那么模式将被应用尽可能多的次数,数组可以是任何长度,并且结尾空字符串将被丢弃。


0 0