1.String类中的substring(int beginIndex)方法:
public String substring(int beginIndex) {
//如果开始截取位置的索引值小于0,抛出异常
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
//如果开始截取位置的索引大于原字符串的长度,抛出异常
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
//如果开始位置的索引为0,返回原字符串;
//如果不为0根据原字符串重新创建一个字符串,
//从索引为beginIndex的位置开始进行截取到原字符串结束
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
这是string类中的字符串截取方法,返回该字符串的子字符串。子字符串以指定索引处的字符开始,并扩展到该字符串的末尾。
示例:
package com.ftl818;
public class Test {
public static void main(String[] args) {
String str="hsghdghsgh";
System.out.println(str.substring(0));
System.out.println(str.substring(4));
}
}
输出:
2.String类中的substring(int beginInde,int endIndexx)方法:
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
和上一个方法一样也是用来截取字符串的,上一个方法只有起始值,没有结束值(默认结束值是元字符串的长度);这个方法有起始值,也有结束值。
示例:
package com.ftl818;
public class Test {
public static void main(String[] args) {
String str="hsghdghsgh";
System.out.println(str.substring(0,5));
System.out.println(str.substring(2,8));
}
}
输出:
3.String类中的 public CharSequence subSequence(int beginIndex, int endIndex)方法:
public CharSequence subSequence(int beginIndex, int endIndex) {
return this.substring(beginIndex, endIndex);
}
该方法和subString(int beginIndex,int endIndex)方法是一样的,返回类型不一样,subString返回的是String,subSequence返回的是实现了CharSequence接口的类,也就是说使用subSequence得到的结果,只能使用CharSequence接口中的方法。不过在String类中已经重写了subSequence,调用subSequence方法,可以直接下转为String对象。
示例:
package com.ftl818;
public class Test {
public static void main(String[] args) {
String str="hsghdghsgh";
System.out.println(str.substring(2,8));
System.out.println(str.subSequence(2, 8));
}
}
结果:
4.String类中的concat(String str)方法:
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
将指定的字符串连接到该字符串的末尾。
如果参数字符串的长度是o,则返回这个string对象。否则,返回一个Stringt,它表示一个字符序列,该字符序列是由这个String对象表示的字符序列和由参数字符串表示的字符序列的连接。
示例:
package com.ftl818;
public class Test {
public static void main(String[] args) {
String str="hsghdghsgh";
System.out.println(str.concat(""));
System.out.println(str.concat("添加什么呢?"));
}
}
输出:
注意:如果concat(null)会报空指针异常
5.String类中的replace(char oldChar, char newChar)方法:
public String replace(char oldChar, char newChar) {
//传入需要改变的字符和修改后的字符
//判断需要改变的字符和修改后的字符是否相同,如果相同,则直接返回原字符串
if (oldChar != newChar) {
//如果需要改变的字符和修改后的字符不相同,进行如下操作
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
//先判断原字符串中是否有字符的值为oldChart
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
//如果原字符串中没有字符的值为oldChart,则此时i=len;
//如果原字符串中有字符的值为oldChart,那么此时i的值为第一次出现oldChart的位置的索
//引
if (i < len) {
//创建一个字符数组用来存储字符,数组的长度与原字符串的长度相等
char buf[] = new char[len];
//从0到i的这段字符串中没有值和oldChar相等,所以直接让其等于原字符串
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
//此时开始存在与oldChar值相等的字符了,所以写入字符数组之前要判断
while (i < len) {
char c = val[i];
//如果该值与oldChar相等则将newChar 写入到数组中,如果不相等还写入原值
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
//将生成的数组转化成字符串返回
return new String(buf, true);
}
}
return this;
}
将字符串中的字符换成另一种字符,将得到的新字符串返回。
示例:
package com.ftl818;
public class Test {
public static void main(String[] args) {
String str="hsghdghsgh";
String str2=str.replace('g', 'A');
System.out.println(str2);
}
}
输出:
6.String类中的replaceFirst(String regex, String replacement)方法:
public String replaceFirst(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}
用给定的替换项替换匹配给定正则表达式的字符串的第一个子字符串。
注意,替换字符串中的反斜杠(\)和美元符号($)可能会导致结果不同于将其视为文字替换字符串;如果需要,请参阅如何隐藏这些字符的特殊含义。
示例:
package com.ftl818;
public class Test {
public static void main(String[] args) {
String str="hsghdghsgh";
System.out.println(str.replaceFirst("gh", "三国"));
}
}
输出:
该方法是用来替换子字符串的,替换在第一次在原字符串中出现的位置
7.String类中的replaceAll(String regex, String replacement)方法:
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
和上一个方法一样都是替换原字符串中的子字符串的,不一样的是,上个方法只替换第一次出现的子字符串,而该方法是将所有的子字符串全部替换。
示例:
package com.ftl818;
public class Test {
public static void main(String[] args) {
String str="hsghdghsgh";
System.out.println(str.replaceAll("gh", "三国"));
}
}
输出:
8.String类中的replace(CharSequence target, CharSequence replacement)方法:
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列。
替换从字符串开始到结束,例如,在字符串“aaa”中用“b”替换“aa”将导致“ba”而不是“ab”。
示例:
package com.ftl818;
public class Test {
public static void main(String[] args) {
String str="hsghdghsgh";
System.out.println(str.replaceAll("gh", "三国"));
String str1="aaa";
System.out.println(str1.replaceAll("aa", "b"));
System.out.println(str1.replace("aa", "b"));
}
}
输出:
暂未发现replace(CharSequence target, CharSequence replacement)方法与replaceAll(String regex, String replacement)方法有什么不同。
在网上查了一下,有人是这样说的:
replace是单纯的替换字符串,而replaceAll是替换匹配的正则表达式。
以下为JDK API的doc说明:
replace(CharSequence target, CharSequence replacement) 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
replaceAll(String regex, String replacement) 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
(好吧,我还是没有读出有什么不同)