菜鸟话Java---String的使用

创建字符串

String 类有 11 种构造方法,这些方法提供不同的参数来初始化字符串,下面是常用的:

1
2
3
4
5
6
7
8
9
//直接创建
String str1 = "这个字符串";
//使用字符串创建
String str2 = new String("这个字符串");
//使用数组
char[] helloArray = { 'r', 'u', 'n', 'o', 'o', 'b'};
String helloString = new String(helloArray);

...

当n个字符串直接由+连接起来组成新的字符串时,不是分别创建n+1个String对象。而是直接将合成一个字符串,即只创建了一个字符串对象

1
String a = "abc"+"123"+"567";

当分别创建字符串变量,然后使用+连接起来时

1
2
3
4
5
6
7
8
String a = "abc";
String a1 = "123";
String b1 = a+a1;

//会转化为
String a = "abc";
String a1 = "123";
(new StringBuilder()).append(a).append(a1).toString();

注意:String是不可变字符串,如果需要对字符串做很多修改,那么应该选择使用 StringBufferStringBuilder

String的常用方法

length()

返回字符串的长度

concat

连接字符串,例如string1.concat(string2);

char charAt(int index)

返回指定索引处的 char 值。

boolean endsWith(String suffix)

测试此字符串是否以指定的后缀结束。

indexOf(…)

1
2
3
4
5
6
7
8
9
10
11
int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。

int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。

int indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引。

int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。

intern()

转载这里

  今天在看一本书的时候注意到一个String的intern()方法,平常没用过,只是见过这个方法,也没去仔细看过这个方法。所以今天看了一下。个人觉得给String类中加入这个方法可能是为了提升一点点性能,因为从常量池取数据比从堆里面去数据要快一些。(个人感觉)

  API上的那几句关于这个方法,其实总结一句就是调用这个方法之后把字符串对象加入常量池中,常量池我们都知道他是存在于方法区的,他是方法区的一部分,而方法区是线程共享的,所以常量池也就是线程共享的,但是他并不是线程不安全的,他其实是线程安全的,他仅仅是让有相同值的引用指向同一个位置而已,如果引用值变化了,但是常量池中没有新的值,那么就会新开辟一个常量结果来交给新的引用,而并非像线程不同步那样,针对同一个对象,new出来的字符串和直接赋值给变量的字符串存放的位置是不一样的,前者是在堆里面,而后者在常量池里面,另外,在做字符串拼接操作,也就是字符串相”+”的时候,得出的结果是存在在常量池或者堆里面,这个是根据情况不同不一定的,我写了几行代码测试了一下。

先上结果:

  1. 直接定义字符串变量的时候赋值,如果表达式右边只有字符串常量,那么就是把变量存放在常量池里面。
  2. new出来的字符串是存放在堆里面。
  3. 对字符串进行拼接操作,也就是做”+”运算的时候,分2中情况:

i. 表达式右边是纯字符串常量,那么存放在栈里面。

ii. 表达式右边如果存在字符串引用,也就是字符串对象的句柄,那么就存放在堆里面。

1
2
3
4
5
6
7
8
    String str1 = "aaa";
String str2 = "bbb";
String str3 = "aaabbb";
String str4 = str1 + str2;
String str5 = "aaa" + "bbb";
System.out.println(str3 == str4); // false
System.out.println(str3 == str4.intern()); // true
System.out.println(str3 == str5);// true

结果:str1、str2、str3、str5都是存在于常量池,str4由于表达式右半边有引用类型,所以str4存在于堆内存,而str5表达式右边没有引用类型,是纯字符串常量,就存放在了常量池里面。

lastIndexOf(…)

1
2
3
4
5
6
7
8
9
10
11
int (int ch)
返回指定字符在此字符串中最后一次出现处的索引。
int lastIndexOf
int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。

int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引。

int lastIndexOf(String str, int fromIndex)
返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。

boolean matches(String regex)

告知此字符串是否匹配给定的正则表达式

replaceXXX()

1
2
3
4
5
6
7
8
9

String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。

String replaceFirst(String regex, String replacement)
使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。

split()

转载菜鸟教程

split() 方法根据匹配给定的正则表达式来拆分字符串。

注意: . 、 | 和 * 等转义字符,必须得加 \。

注意:多个分隔符,可以用 | 作为连字符。

语法:

1
2
3
4
5
6
public String[] split(String regex, int limit)
参数
regex -- 正则表达式分隔符。
limit -- 分割的份数。
返回值
字符串数组。

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Test {
public static void main(String args[]) {
String str = new String("Welcome-to-Runoob");

System.out.println("- 分隔符返回值 :" );
for (String retval: str.split("-")){
System.out.println(retval);
}

System.out.println("");
System.out.println("- 分隔符设置分割份数返回值 :" );
for (String retval: str.split("-", 2)){
System.out.println(retval);
}

System.out.println("");
String str2 = new String("www.runoob.com");
System.out.println("转义字符返回值 :" );
for (String retval: str2.split("\\.", 3)){
System.out.println(retval);
}

System.out.println("");
String str3 = new String("acount=? and uu =? or n=?");
System.out.println("多个分隔符返回值 :" );
for (String retval: str3.split("and|or")){
System.out.println(retval);
}
}
}

以上程序执行结果为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 分隔符返回值 :
Welcome
to
Runoob

- 分隔符设置分割份数返回值 :
Welcome
to-Runoob

转义字符返回值 :
www
runoob
com

多个分隔符返回值 :
acount=?
uu =?
n=?

String trim()

返回字符串的副本,忽略前导空白和尾部空白。

substring(…)

1
2
3
4
5
String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。

String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。

更多Java String的方法见api