//String类的常用方法
//简化输出格式
public static void sop(Object obj)
{
System.out.println(obj);
}
String s1="abcbde";
sop(s1.length());
sop(s1.charAt(3));
sop(s1.indexOf('b'));
sop(s1.indexOf('b',2));
sop(s1.indexOf("bc"));
sop(s1.lastIndexOf('b'));
String s="abcde";
sop(s.contains("bcd"));
sop(s.startsWith("abc"));
sop(s.endsWith("de"));
String s1="abCde";
sop(s.equals(s1));
sop(s.equalsIgnoreCase(s1)); //忽略大小写
char[] ch={'a','b','c','d','e'};
//char->string
//构造方法
String s=new String(ch);
String s1=new String(ch,2,2);//第一个2是索引,第二个2是个数
sop(s);
sop(s1);
//静态方法
String s2=String.copyValueOf(ch);
String s3=String.copyValueOf(ch,2,2);
sop(s2);
sop(s3);
//string->char
String s4="abcdef";
char[] ch1=s4.toCharArray();
for (int x=0;x<ch1.length ;x++ )
{
sop(ch1[x]);
}
//将字节数组转成字符串。
//String(byte[])
//String(byte[],offset,count):将字节数组中的一部分转成字符串。
//将字符串转成字节数组。
//byte[] getBytes():
//将基本数据类型转成字符串。
//static String valueOf(int)
//static String valueOf(double)
3+"";//String.valueOf(3);
//特殊:字符串和字节数组在转换过程中,是可以指定编码表的。
String s="haha java";
sop(s.replace('a','o'));
sop(s.replace("java","haha"));
String s = "wohao haha hss";
String[] arr = s.split(" ");
for(int x = 0; x<arr.length; x++)
{
sop(arr[x]);
}
String s="abcde";
sop(s.substring(1)); //等价于sop(s.substring(1,s.length()));
sop(s.substring(1,3));//取到头,取不到尾:bc
String s=" ab C de ";
sop(s.toUpperCase());
sop(s.toLowerCase());
sop(s.trim()); //去掉两端所有空格
String s1="abc";
String s2="aaa";
sop(s1.compareTo(s2));