1、split的使用:
String str = "this is string example....wow!!!"
print(str.split(" "));//根据空格进行切割
print(str.split("i", 3));//根据i 切割成三份
print(str.split("w"));//根据w进行切割
输出结果:
['this', 'is', 'string', 'example....wow!!!']
['th', 's' 's string example....wow!!!']
['this is string example....', 'o', '!!!']
2、substring的使用:
var str = "0123456789";
alert(str.substring(-5));-----------"0123456789"
alert(str.substring(0));------------"0123456789"
alert(str.substring(5));------------"56789"
alert(str.substring(10));-----------""
alert(str.substring(0,5));----------"01234"//左闭右开
alert(str.substring(-1,5));----------"012345"
alert(str.substring(2,2));----------""
alert(str.substring(2,5));----------"234" //左闭右开
3、lastindexOf的使用:
String Str = new String("oppor11");
System.out.println(Str.lastIndexOf( 'o' )); //查找字符 o 最后出现的位置
System.out.println(Str.lastIndexOf( 'o', 14 )); //从第14个位置查找字符 o 最后出现的位置
4、字符串替换函数:
(1)replace() 方法 ------ replace() 方法会将字符串中所有 oldChar 替换成 newChar
String words = "hello java,hello php";
words.replace("l","D"));
words.replace("hello","你好 "));
输出结果:
heDDo java,heDDo php
你好 java,你好 php
(2)replaceFirst() 方法 ------将匹配到的第一个字符串替换成新的字符串
String words = "hello java,hello php";
String newStr = words.replaceFirst("hello","你好 ");
System.out.println(newStr);
输出结果:
你好 java,hello php
(3)replaceAll() 方法 ------将目标字符串中的所有匹配到的字符串替换成新的字符串
String words = "hello java,hello php";
String newStr = words.replaceAll("hello","你好 ");
System.out.println(newStr);
输出结果:
你好 java,你好 php

被折叠的 条评论
为什么被折叠?



