java字符串,常用方法

一字符串比较
1.equal()方法:字符串比较,区分大小写,
equalsIgnoreCase()方法,不需要区分大小写的比较,

String str1 = "hello" ; 
String str2 = "Hello" ; 
//结果为false
System.out.println(str1.equals(str2)); 
//结果为true 
System.out.println(str1.equalsIgnoreCase(str2));

2.compareTo()方法,按字典顺序比较两个字符串的Unicode值, 1. 相等:返回0. 2. 小于:返回内容小于0. 3. 大于:返回内容大于0。

 System.out.println("A".compareTo("a")); // -32 
System.out.println("a".compareTo("A")); // 32 
System.out.println("A".compareTo("A")); // 0 
System.out.println("AB".compareTo("AC")); // -1 
System.out.println("刘".compareTo("杨")); //比较结果介于unicode码

二.字符串查找
1,contains()方法

String str = "helloworld" ; 
System.out.println(str.contains("world")); // true
  1. indexOf()方法
    从头开始查找指定字符串的位置,查到了就返回位置的开始索引(从0开始),如果查不到,就返回-1。
String str = "helloworld" ; 
System.out.println(str.indexOf("world")); // 5,w开始的索引
System.out.println(str.indexOf("bit")); // -1,没有查到

3.startsWith()和endsWith()方法
true如果由参数表示的字符序列是由该对象表示的字符序列的后缀; false否则。 注意,结果将是true如果参数是空字符串或等于该String如由所确定的对象equals(Object)方法。

String str = "**@@helloworld!!" ; 
System.out.println(str.startsWith("**")); // true 
System.out.println(str.startsWith("@@",2)); // ture 
System.out.println(str.endsWith("!!")); // true

三,字符串替换
1.replaceAll()方法

String str = "helloworld" ; 
System.out.println(str.replaceAll("l", "_")); //he__owor_d
System.out.println(str.replaceFirst("l", "_")); //he_loworld

四,字符串拆分
1.split()方法

String str = "hello world hello People" ; 
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) { 
 System.out.println(s); 
} 
//hello
//word
//hello
//people
String str = "192.168.1.1" ; 
String[] result = str.split("\\.") ; 
for(String s: result) { 
 System.out.println(s); 
}
//192
//168
//1
//1
String str = "name=zhangsan&age=18" ; 
String[] result = str.split("&") ; 
for (int i = 0; i < result.length; i++) { 
 String[] temp = result[i].split("=") ; 
 System.out.println(temp[0]+" = "+temp[1]); 
}
//name=zhangsan
//age=18

五,字符串截取
1.substring()方法

String str = "helloworld" ; 
System.out.println(str.substring(5)); // world
System.out.println(str.substring(0, 5));//hello

六,字符串其他常见的操作方法
这些常见的操作方法比如:取得字符串的长度,去掉字符串两边的空格,保留中间的空格,字符串大小写转换,字符串反转。

1,trim()方法:去掉字符串两边的空格

String str = " hello world " ; 
System.out.println("["+str+"]"); 
System.out.println("["+str.trim()+"]"); 
//[hello world ]
//[hello world]

2.toUpperCase()和toLowerCase()方法;字符串大小写转换

String str = " hello%$$%@#$%world 哈哈哈 " ; 
System.out.println(str.toUpperCase()); // HELLO%$$%@#$%WORLD 哈哈哈 
System.out.println(str.toLowerCase()); // hello%$$%@#$%world 哈哈哈 

3,length()方法:取得字符串的长度

String str = " hello%$$%@#$%world 哈哈哈 " ; 
System.out.println(str.length()); //24

4.reverse()方法:字符串反转。

StringBuffer sb = new StringBuffer("helloworld"); 
System.out.println(sb.reverse()); //dlrowolleh


[参考]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值