您的位置:首页 > 其它

String字符串常用方法总结

2017-06-28 16:44 302 查看
通过跑main方法来加深String印象

//String类为不可变字符序列
//String类内部实质是数组private final char value[];
//字符串+数字返回仍是字符串
//new String的构造对象比直接赋值对象多创建一个对象,通过内存分析可得
public class TestString {

public static void main(String[] args) {
String s="abc";
String s1=new String("def");
String s2="abcdabcdab";
String s3="a,s,cde";
System.out.println(s.charAt(0));    //返回a
System.out.println(s.indexOf("a")); //返回0
System.out.println(s.lastIndexOf("c")); //返回2
System.out.println(s2.indexOf("a",2));  //返回4
System.out.println(s.concat(s1));   //返回abcdef
System.out.println(s.contains(s1)); //返回false
System.out.println(s.substring(0)); //返回abc
System.out.println(s.substring(0, 1));  //返回a
System.out.println(s2.replace('a','m' ));//返回mbcmbcdmb
System.out.println(s2.endsWith("ab"));  //返回true
System.out.println(s2.startsWith("a")); //返回true
System.out.println(s1.getBytes());      //返回[B@15db9742
String s4[]=s3.split(",");
for(int i=0;i<s4.length;i++){
System.out.print(s4[i]+"\t");       //返回a           s           cde
}
System.out.println();
char c[]={'a','b','c'};
System.out.println(String.copyValueOf(c));  //将字符数组转为字符串
System.out.println(s.toUpperCase());        //返回ABC
System.out.println(String.valueOf(50));     //返回字符串50
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string