public class StringClassTest { public static void main(String[] args) { //遍历字符串 String str = "Hello world"; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); System.out.print(ch+" "); } System.out.println(); //在字符串里查找子串并且返回其字串的第一个字母的小标,么找到返回-1 System.out.println(str.indexOf("llo")); System.out.println(str.indexOf("all")); //在字符串查找某个字符,找到返回下标,么找到返回-1 System.out.println(str.indexOf(‘l‘)); System.out.println(str.indexOf(‘a‘)); //给出要输出的字符串的起始小标和最终下标,输出字符串 String sub = str.substring(0,str.length()); System.out.println(sub); } }
原文:https://www.cnblogs.com/128-cdy/p/11717067.html