indexOf()
-
public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
// 用例:
String str = "<Hello World>"; int indexOf = str.indexOf("<"); int of = str.indexOf(">"); String substring = str.substring(indexOf+1, of); String replaceAll = str.replaceAll("<", ">"); System.out.println(replaceAll);
replaceAll()
- replaceAll() 方法使用给定的参数 replacement 替换字符串所有匹配给定的正则表达式的子字符串(regex)。
-
返回值: 成功则返回替换的字符串,失败则返回原始字符串。
// 用例:
String str = new String("www.google.com"); System.out.println(str.replaceAll("www","123")); System.out.println(str.replaceAll("(.*)google(.*)", "123"));