String类
String类实例化
String s1="hello";
String s2=new String("world");
char[] chars={'r','e','f','i','n','e'};
String s4=new String(chars);
String不同拼接操作
- 常量与常量的拼接结果在常量池;常量池不会存在相同内容;
- 只有有一个结果是变量,结果就在堆中;
String s="HelloWorld";
final String s1="Hello";
String s2="Hello";
String s3="World";
String s4 = s1 + "World";
String s5="Hello"+"World";
String s6=s2+"World";
String s7=s2+s3;
System.out.println(s==s4);
System.out.println(s4==s5);
System.out.println(s==s5);
System.out.println(s==s6);
System.out.println(s==s7);
System.out.println(s6==s7);
String类常用的方法
String s=" Refine ";
String s1="refine";
String s2=new String("refine");
System.out.println(s.length());
- charAt(int n):返回指定索引所在的字符
System.out.println(s.charAt(2));
System.out.println(s.isEmpty());
System.out.println(s.toLowerCase());
System.out.println(s.toUpperCase());
- trim():去除字符串前后空格,返回一个新的字符串
String s3=s.trim();
System.out.println(s.trim());
System.out.println(s1.equals(s2));
- equalsIgnoreCase():忽略大小写判断两个字符串是否相等
System.out.println(s.trim().equalsIgnoreCase(s1));
System.out.println(s.concat("sophia"));
System.out.println(s.compareTo(s1));
- substring():截取字符串,返回一个子字符串
System.out.println(s1.substring(2));
- endsWith():判断字符串是否以某个字符结尾
String s="Hello World!";
System.out.println(s.endsWith("!"));
- startsWith():判断字符串是否以某个字符开头
System.out.println(s.startsWith("e"));
System.out.println(s.startsWith("e", 1));
- contains():判断字符串是否包含指定字符、字符串
System.out.println(s.contains("el"));
System.out.println(s.indexOf("l"));
- lastIndexOf():返回指定字符最后一次出现的索引位置
System.out.println(s.lastIndexOf("e", 4));
- replace():使用指定字符串C替换字符串A中原有字符串B
String s=new String("Hello world!上海");
System.out.println(s.replace("l","L"));
System.out.println(s.replace("上海", "安徽"));
- replaceAll():正则表达式的方式替换原有字符串
String s1="3433mysql123linux343java234python4556jmeter458";
String s2 = s1.replaceAll("\\d+", ",");
String s3 = s2.replaceAll("^,|$,", "");
System.out.println(s3);
String类与char[]相互转换
String类型转换为char[]
String s=new String("Hello world!上海");
char[] chars=s.toCharArray();
for (char ch:chars) {
System.out.println(ch);
}
char[]转为String
char[] chars1=new char[]{'a','b','c'};
String s1=new String(chars1);
System.out.println(s1);
### String类与bytes[]相互转换
String类型转换为bytes[]
String s=new String("Hello world!得到");
byte[] bytes = s.getBytes();
System.out.println(Arrays.toString(bytes));
byte[] bytes1=null;
try {
bytes1 = s.getBytes("gbk");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
System.out.println(Arrays.toString(bytes1));
bytes[]类型转换为String类型
String s1 = new String(bytes);
System.out.println(s1);
String s2 = null;
try {
s2 = new String(bytes1,"gbk");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
System.out.println(s2);
String类、StringBuffer类与StringBuilder类的比较
- String:是不可变的字符序列;底层使用char[]存储
- StringBuffer:可变的字符序列;线程安全的,效率低;底层使用char[]存储
- StringBuilder:可变的字符序列;线程不安全的,效率高;底层使用char[]存储
StringBuffer常用的方法
StringBuffer stringBuffer=new StringBuffer("Refine");
stringBuffer.append('D');
stringBuffer.append("David");
- delete():删除指定开始索引、结束索引位置的字符串;
stringBuffer.delete(7,8);
System.out.println(stringBuffer.replace(8,9,"LL"));
stringBuffer.insert(0,"HELLO");
System.out.println(stringBuffer.reverse());
System.out.println(stringBuffer.indexOf("H"));
- substring():截取指定索引位置的字符串,返回为一个新的字符串
System.out.println(stringBuffer.substring(2,5));
System.out.println(stringBuffer.length());
System.out.println(stringBuffer.charAt(1));
stringBuffer.setCharAt(1,'U');