字符串比较

字符串比较

1. 比较是否同一字符串对象

String str1=new String("hello wolrd");

String str2=new String("hello wolrd");

boolean b=(str1==str2); //返回false

2. 比较字符串的内容

boolean b=(str1.equals(str2));

//返回 true

3. 比较字符串的内容并且忽略大小写

boolean b=(str1.equalsIgnoreCase(str2));

//返回true

4. 按字典顺序比较

String str1=new String("hello");

String str2=new String("world");

int x=(str1.compareTo(str2)); //返回-15

拆分字符串

String str=new String("hello world");

for(String s:str.split(" "))

System.out.println(s);

输出结果:

hello

world

猜你喜欢

转载自blog.csdn.net/dwenxue/article/details/78810348