String类的常用方法
1、length()字符串的长度
1 String str=new String("abc"); 2 int len=str.length();
2、charAt() 截取一个字符
1 String str = new String("abc"); 2 char ch = str.charAt(1);
3、equals()和equalsIgnoreCase() 比较两个字符串
1 String str1 = new String("abc"); 2 String str2 = new String("ABC"); 3 int a = str1.compareTo(str2); 4 int b = str1.compareToIgnoreCase(str2);
4、compareTo()和compareToIgnoreCase() 比较字符串
1 String str1 = new String("abc"); 2 String str2 = new String("ABC"); 3 boolean a = str1.equals(str2); 4 boolean b = str1.equalsIgnoreCase(str2);
5、concat() 连接两个字符串
1 String str = "a".concat("b").concat("c");
6、replace() 替换
1 String str = "abcdef"; 2 String str1 = str.replace('a','z');//str1 = "zbcdef"
7、toUpperCase() /toLowerCase() 转换为大小写
1 String str = new String("abcdEF"); 2 String str1 = str.toLowerCase(); 3 String str2 = str.toUpperCase();
8、trim() 去掉起始和结尾的空格
1 String str = " a b c "; 2 String str1 = str.trim(); 3 int a = str.length(); 4 int b = str1.length();