java---String类
package sad; //String 类的常用方法 import java.util.Scanner; public class ers { public static void main(String[] args) { Factory.choose(); } } class Factory { static void choose() { int Choose=0; Scanner sc = new Scanner(System.in); while(true) { System.out.println("请输入您要的操作:"); System.out.print("1:提取字符串中的某个字符\n2:字符串和数组相互转换\n3:判断是否包含某个字符串(查找)\n4:字符串替换\n5:截取字符串\n6:拆分字符串\n"); Choose= sc.nextInt(); switch(Choose) { case 1: new StringCharAt(); break; case 2: new StringArray(); break; case 3: new StringSearch(); break; case 4: new StringReplace(); break; case 5: new SubString(); break; case 6: new StringSplit(); break; default: break; } } } } class StringCharAt { public StringCharAt() { Scanner sc = new Scanner(System.in); System.out.println("请输入字符串:"); String str = sc.next(); System.out.println("第零个字符是:"+str.charAt(0)); //取出字符串中的某个字符 System.out.println("第二个字符是:"+str.charAt(2)); } } class StringArray { public StringArray() { Scanner sc = new Scanner(System.in); System.out.println("请输入字符串:"); String str = sc.next(); char data[] = str.toCharArray(); //字符串转数组 System.out.println("该字符串转换为数组后为:"); for(int i=0;i<data.length;i++) { System.out.println(data[i]+" "); } System.out.println("数组转换为字符串后为:"); System.out.println(new String(data)); //数组转字符串 System.out.println("从数组的某个字符开始截取某个长度的字符串:"); System.out.println("请输入起始位置:"); int start = sc.nextInt(); System.out.println("请输入长度:"); int length = sc.nextInt(); System.out.println(new String(data,5,4));//起始点,长度 System.out.println("____________________"); } } class StringSearch { public StringSearch() { // TODO Auto-generated constructor stub String str = "***hello**word"; if(str.contains("hello")) //判断是否包含某个字符串 { System.out.println("包含指定的字符串"); } if(str.indexOf("e")!=-1) //从头开始判断是否存在指定字符 ,返回值是位置 { System.out.println("字符串位置:"+str.indexOf('e')); } if(str.indexOf("e", 6)==-1) //从指定位置开始向后查找是否存在某个字符 { System.out.println("不存在"); } if(str.lastIndexOf("e",12)==-1) //从指定位置开始从后查找是否存在某个字符 { System.out.println("不存在"); } if(str.startsWith("*")) //判断是否以某个字符开头 { System.out.println("true"); } if(str.startsWith("*",2)) //判断指定位置是否为某个字符或某一段字符 { System.out.println("true"); } if(str.endsWith("d")) //判断是否由某个字符结束 { System.out.println("true"); } System.out.println("____________________"); } } class StringReplace { StringReplace() { String str = "helloword"; System.out.println(str.replaceAll("o", "***")); //将出现的某个字符所有的都替换 System.out.println(str.replaceFirst("l", "_")); //将第一个出现的指定字符替换 System.out.println("____________________"); } } class SubString { public SubString() { String str = "helloJava"; System.out.println(str.substring(5)); //截取从指定位置到结尾的字符串 System.out.println(str.substring(0,5));//截取指定位置的字符串 System.out.println("____________________"); } } class StringSplit { public StringSplit() { String str = "hello world hello java"; String[] data = str.split(" "); for(int i=0;i<data.length;i++) { System.out.println(data[i]); } System.out.println("____________________"); String data2[] = str.split(" ",3); for(int i=0;i<data2.length;i++) { System.out.println(data2[i]); } } }