1、String的两种实例化方式
①String str = new String("张三");
②String str = "张三";
两种实例化方式的比较:
第一种方式:会在堆和栈内存各开辟一块空间
第二种方式:会在堆内存开辟一块空间
2、String类常用方法
public class StringMethodTest {
public static void main(String[] args){
//1.第一种实例化方式
String str1 = new String("张三");
//2.第二种实例化方式
String str2 = "张三";
System.out.println(str1 + "----" + str2);
/**
* String类常用方法
* charAt(int index) toCharArray() getBytes() startsWith(String pre)
* endsWith(String suff) replace(String target,String replacement)
* substring(int beginIndex) substring(int beginIndex,int endIndex)(包前不包后)
* indexOf(String str) indexOf(String str,int fromIndex) contains(String str)
* trim() length() toUpperCase() toLowerCase()
*/
//测试charAt(int index)方法
System.out.println("-------------------测试charAt(int index)方法-----------------");
String s1 = "abcdce";
char c = s1.charAt(2);
System.out.println(c);
//测试toCharArray()方法
System.out.println("-------------------测试toCharArray()方法-----------------");
char[] c2 = s1.toCharArray();
for(int i=0; i<c2.length; i++){
System.out.print(c2[i]+",");
}
//测试getBytes()方法
System.out.println("-------------------测试getBytes()方法-----------------");
byte[] b = s1.getBytes();
for(int i=0; i<b.length; i++){
System.out.println(b[i]);//得到ASCII码
}
//测试startsWith(String pre)方法
System.out.println("-------------------测试startsWith(String pre)方法-----------------");
if(s1.startsWith("a")){
System.out.println("该字符串以a开始");
}
//测试endsWith(String suff)方法
System.out.println("-------------------测试endsWith(String suff)方法-----------------");
if(s1.endsWith("e")){
System.out.println("该字符串以e结尾");
}
//测试replace(String target,String replacement)方法
System.out.println("-------------------测试replace(String target,String replacement)方法-----------------");
String s2 = s1.replace("c", "f");
System.out.println(s2);
//测试substring(int beginIndex)方法
System.out.println("-------------------测试substring(int beginIndex)方法-----------------");
String s3 = s1.substring(2);
System.out.println(s3);
//测试substring(int beginIndex,int endIndex)方法
System.out.println("-------------------测试substring(int beginIndex,int endIndex)方法-----------------");
String s4 = s1.substring(1,3);
System.out.println(s4);//包前不包后
//测试indexOf(String str)方法
System.out.println("-------------------测试indexOf(String str)方法-----------------");
int index = s1.indexOf(c);
System.out.println(index);
//测试indexOf(String str,int formIndex)方法
System.out.println("-------------------测试indexOf(String str,int formIndex)方法-----------------");
int index2 = s1.indexOf(c,3);
System.out.println(index2);
//测试contains(String str)方法
System.out.println("-------------------测试contains(String str)方法-----------------");
if(s1.contains("c")){
System.out.println("该字符串包含c");
}
//测试trim()方法
System.out.println("-------------------测试trim()方法-----------------");
String str = " abcde ";
System.out.println(str.trim());
//测试length()方法
System.out.println("-------------------测试length()方法-----------------");
System.out.println(str.length());
//测试toUpperCase()方法
System.out.println("-------------------测试toUpperCase()方法-----------------");
System.out.println(s1.toUpperCase());
//测试toLowerCase()方法
System.out.println("-------------------测试toLowerCase()方法-----------------");
System.out.println(s1.toUpperCase().toLowerCase());
}
}