[size=medium]1. split()[/size]
根据特定字符,把一个字符串分解成n个部分。
有两种形式:
split(regex)调用split(regex, 0)方法
limit控制着分解的次数,所以对结果有影响:
a. 如果limit是正数,分解最多(limit - 1)次
b. 如果limit是负数,分解次数没有限制
c. 如果limit是0,分解次数没有限制,但是会去除结果尾部空的部分
举例:
结果:
arrSplited0: [, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
arrSplitedA: [, 0, 1, 2,3,4,5,6,7,8,9,,]
arrSplitedB: [, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, , ]
arrSplitedC: [, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[size=medium]2. String是一个常量[/size]
String是不可被改变的,一旦创建某个String,其它地方只需对它进行引用; 通过new String(String str)方式会创建一个新的String对象:
如果是两个或两个以上的字符串组合,编译器会对它们进行优化,组成一个字符串,效果和上面相同:
但是运行时才知道的String除外:
[size=medium]3. intern()[/size]
该方法返回一个字符串对象的内部化引用。
String类维护一个初始为空的字符串的对象池,当intern方法被调用时,如果对象池中已经包含这一个相等的字符串对象则返回对象池中的实例,否则添加字符串到对象池并返回该字符串的引用。
szCanonicalA和szCanonicalB都是常量(szCanonicalB的值会在编译时候被优化)
[size=medium]4. concat()[/size]
把指定的字符串附加到当前字符串的末尾。str为null时会产生NullPointerException异常。
和strA + strB相比,strA.concat(strB)一定程度上提升了性能。
[size=medium]5. toString()[/size]
如果某Object对象为null,调用toString()会产生NullPointerException异常,可以通过两种方式来避免:
a. 判断:
b. 如果obj为null,强制转化后还是为null,不会产生异常:
[size=medium]6. equals()[/size]
比较当前String对象是否和指定的Object对象的String形式内容相同。参数anObject可以为null:
[size=medium]7. equalsIgnoreCase()[/size]
和equals()类似,区别是这里不区分大小写:
[size=medium]8. replace系列[/size]
例子:
[size=medium]9. matches[/size]
java.util.regex.Pattern的matches方法:
根据特定字符,把一个字符串分解成n个部分。
有两种形式:
split(String regex);
split(String regex, int limit);
split(regex)调用split(regex, 0)方法
limit控制着分解的次数,所以对结果有影响:
a. 如果limit是正数,分解最多(limit - 1)次
b. 如果limit是负数,分解次数没有限制
c. 如果limit是0,分解次数没有限制,但是会去除结果尾部空的部分
举例:
String szToSplit = ",0,1,2,3,4,5,6,7,8,9,,";
String[] arrSplited0 = szToSplit.split(",");
String[] arrSplitedA = szToSplit.split(",", 4);
String[] arrSplitedB = szToSplit.split(",", -1);
String[] arrSplitedC = szToSplit.split(",", 0);
结果:
arrSplited0: [, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
arrSplitedA: [, 0, 1, 2,3,4,5,6,7,8,9,,]
arrSplitedB: [, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, , ]
arrSplitedC: [, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[size=medium]2. String是一个常量[/size]
String是不可被改变的,一旦创建某个String,其它地方只需对它进行引用; 通过new String(String str)方式会创建一个新的String对象:
String heaven1 = "paradise";
String heaven2 = "paradise";
String heaven3 = new String("paradise");
System.out.println(heaven1 == heaven2); // true
System.out.println(heaven1 == heaven3); // false
如果是两个或两个以上的字符串组合,编译器会对它们进行优化,组成一个字符串,效果和上面相同:
String heaven4 = "discriminator" + "paradise";
String heaven5 = "discriminator" + "paradise";
System.out.println(heaven4 == heaven5); // true
但是运行时才知道的String除外:
public class StringParadise {
private String key;
private String value;
// Getters and setters are omitted
public StringParadise(String key1, String key2, String value) {
this.key = key1 + key2;
this.value = value;
}
public static void main(String[] args) {
StringParadise test1 = new StringParadise("a", "", "paradise");
StringParadise test2 = new StringParadise("a", "", "heaven");
System.out.println(test1.getKey() == test2.getKey()); // false
}
}
[size=medium]3. intern()[/size]
该方法返回一个字符串对象的内部化引用。
String类维护一个初始为空的字符串的对象池,当intern方法被调用时,如果对象池中已经包含这一个相等的字符串对象则返回对象池中的实例,否则添加字符串到对象池并返回该字符串的引用。
String strCanonicalA = "Hello, Inteference";
String strCanonicalB = new String("Hello, Inteference");
System.out.println(strCanonicalA == strCanonicalB); // false
String strInternB = strCanonicalB.intern();
System.out.println(strCanonicalA == strInternB);
szCanonicalA和szCanonicalB都是常量(szCanonicalB的值会在编译时候被优化)
[size=medium]4. concat()[/size]
把指定的字符串附加到当前字符串的末尾。str为null时会产生NullPointerException异常。
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
char buf[] = new char[count + otherLen]; // 实例化字符数组
getChars(0, count, buf, 0); // 把当前字符串的字符序列放到数组上
str.getChars(0, otherLen, buf, count); // 把参数字符串的字符序列附加到数组上
return new String(0, count + otherLen, buf); // 构建新的字符串
}
和strA + strB相比,strA.concat(strB)一定程度上提升了性能。
[size=medium]5. toString()[/size]
如果某Object对象为null,调用toString()会产生NullPointerException异常,可以通过两种方式来避免:
a. 判断:
Object obj = null;
if (obj != null) {
String str = obj.toString();
}
b. 如果obj为null,强制转化后还是为null,不会产生异常:
Object obj = null;
String str = (String)obj;
[size=medium]6. equals()[/size]
比较当前String对象是否和指定的Object对象的String形式内容相同。参数anObject可以为null:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
[size=medium]7. equalsIgnoreCase()[/size]
和equals()类似,区别是这里不区分大小写:
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true :
(anotherString != null) && (anotherString.count == count) &&
regionMatches(true, 0, anotherString, 0, count);
}
[size=medium]8. replace系列[/size]
// regex为正则表达式,替换第一个匹配的字符串
// replacement中含有'\'或'$',会当成转义或特殊含义处理而被忽略。
public String replaceFirst(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}
// 同上,替换所有匹配的字符串
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
// 替换所有匹配的字符序列
// Pattern.LITERAL指定target只是普通字符串,不是正则表达式。
// 因为调用了Matcher的quoteReplacement方法,所以'\'或'$'会当作普通字符处理。
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
// 替换所有匹配的字符
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = count;
int i = -1;
char[] val = value; /* avoid getfield opcode */
int off = offset; /* avoid getfield opcode */
while (++i < len) {
if (val[off + i] == oldChar) { // 寻找第1个匹配
break;
}
}
if (i < len) {
char buf[] = new char[len]; // 创建字符数组
for (int j = 0 ; j < i ; j++) {
buf[j] = val[off+j]; // 拷贝第1个匹配之前的字符
}
while (i < len) { // 拷贝第1个匹配(包括)后面的字符
char c = val[off + i];
buf[i] = (c == oldChar) ? newChar : c; // 匹配就替换,反之拷贝
i++;
}
return new String(0, len, buf); // 转化为字符串
}
}
return this;
}
例子:
String str = "玉立gorgeous_%520";
//str = str.replace('r', '*'); // 玉立go*geous_%520
//str = str.replaceFirst("\\w", "*"); // 玉立*orgeous_%520
//str = str.replaceAll("\\w", "*"); // 玉立*********%***
//str = str.replaceAll("[\\w\u4e00-\u9fa5]", "*"); // ***********%***
//str = str.replaceAll("\\w", "$"); // StringIndexOutOfBoundsException
str = str.replace("\r|\n", ""); // 去除回车换行符
[size=medium]9. matches[/size]
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
java.util.regex.Pattern的matches方法:
public static boolean matches(String regex, CharSequence input) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
}