java的string常用操作

时间:2023-11-28 19:04:50
import java.util.*;
public class Demo3 {
public static void main(String args[]){
String str = "hello world I love python!";
//字符串长度
System.out.println("字符串长度:"+str.length());
//字符串索引查询
System.out.println(str.charAt(7));
//字符串内部元素查询
System.out.println(str.contains("I"));
//字符串替换内部元素
System.out.println(str.replace("I", "Lilei"));
//字符串切割,切割结果是一个数组
String[] strArr = str.split(" ");
System.out.println(Arrays.toString(strArr));
}
}

  结果:

字符串长度:26
o
true
hello world Lilei love python!
[hello, world, I, love, python!]