Java's common methods about String (string to character array, character array to string)

1. String to string array

String str="abcde";
char[]ch =str.toCharArray();

2. String array to string

//将数组转成字符串~~~~
String[] aa ={"z","s","l","s"};
String s = Arrays.toString(aa);

3. Convert character array to string

//方法一:
String str = "abcde";
char[] ch = str.toCharArray();
String s = String.valueOf(ch);
System.out.println(s);
//方法二:
char data[] = {'a', 'b', 'c'};
String str = new String(data);

4. Intercept the string to specify the index start

String str = "abcde";
String s = str.substring(1, 3);//注意区间是左开右闭

5. Convert the string to case

String str = "abcde";
String s = str.toUpperCase();
String s = str.toLowerCase();

6. Regular segmentation of split in String

public String[] split(String regex)
//regex为给定的需要分割字符串规则
例如,字符串"boo:and:foo"使用以下表达式得到以下结果: 

Regex Result 
: { "boo", "and", "foo" } 
o { "b", "", ":and:f" } 

7. Replace a specified character in String

public String replace(char oldChar,char newChar);
//例子
 "mesquite in your cellar".replace('e', 'o')
   结果为: "mosquito in your collar"

Guess you like

Origin blog.csdn.net/qq_63802547/article/details/127839612