public class Mm {
public static void main(String[] args) {
//字符串实例化
String a="天王盖地虎";//直接引用字符串常量
String b="宝塔镇河妖";
String c=new String(b);//使用已有字符串变量实例化
String d=new String("有木有段友");//利用构造方法实例化
char ch[]={'轻','风','吹','杨','柳'};
String e=new String(ch);//利用字符数组实例化
String f=new String(ch,2,3);//提取字符数组部分内容,从小标为2开始,提取3个字符
System.out.println(a+","+b);
System.out.println(e+","+d);
System.out.println(c+","+f);
//连接字符串
String g="你不太会说话,";
String h="所以才成单身狗";
g+=h;//使用+号
System.out.println(g);
g.concat(h);//使用concat方法将h连接到g的末尾
System.out.println(g);
//提取字符串信息
//1、获取字符串长度
int length=a.length();
System.out.println(length);
//2、获取指定索引位置的字符
char chr=a.charAt(4);
System.out.println(chr);
//3、获取子字符串的索引位置,当不含子字符串时,返回-1
int index=a.indexOf("地虎");//返回子字符串第一次出现的位置
System.out.println(index);
index=a.indexOf("地虎", 4);//从指定索引位置开始返回子字符串第一次出现的位置
System.out.println(index);
index=a.lastIndexOf("地虎");//反向搜索子字符串第一次出现的位置
System.out.println(index);
index=a.lastIndexOf("地虎", 2);//从指定索引位置反向搜索子字符串第一次出现的位置
System.out.println(index);
//4、判断字符串首尾内容
String i="我爱java,java爱我";
boolean bool;
bool=i.startsWith("我爱java");
System.out.println(bool);
bool=i.startsWith("java爱我",7);
System.out.println(bool);
bool=i.endsWith("java爱我");
System.out.println(bool);
//5、获取字符数组
char ch2[];
ch2=i.toCharArray();
System.out.println(ch2[0]);
//6、判断子字符串是否存在
bool=i.contains("java");
System.out.println(bool);
}
}