package ss1;
/**
*String类的获取功能
*public int length() 返回此字符串的长度。
*public char charAt(int index) 返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。
*public int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。
*public int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引。
*public int indexOf(int ch, int fromIndex)返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
*public int indexOf(String str, int fromIndex)返回指定子字符串在此字符串中第一次出现处的索引,
*public String substring(int beginIndex)返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。
*public String substring(int beginIndex,int endIndex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。
*/
public class Menu {
public static void main(String[] args) {
String s = "helloworld";
System.out.println("s.length : "+s.length());
System.out.println("s.charAt : "+s.charAt(2));
System.out.println("s.indexOf : "+s.indexOf('h'));
System.out.println("s.indexOf : "+s.indexOf('w'));
System.out.println("s.indexOf : "+s.indexOf('K'));
System.out.println("s.indexOf : "+s.indexOf("he"));
System.out.println("s.indexOf : "+s.indexOf("wo"));
System.out.println("s.indexOf : "+s.indexOf("hw"));
System.out.println("s.indexOf : "+s.indexOf('l',5));
System.out.println("s.substring : "+s.substring(5));
System.out.println("s.substring : "+s.substring(5,7));
}
}
联系客服