016.2 String

 内容:String方法+练习

#######################################
比较方法:equals()
字符串长度:int length()
字符的位置:int indexOf(ch,fromIndex);
获取指定位置上的字符:char charAt(int)
获取部分字符串:String substring(int start,int end);
字符串变成数组:toCharArray()

String方法查找练习:
         * 1,字符串是否以指定字符串开头。结尾同理。
         *         boolean startsWith(string)
         *         boolean endsWith(string)
         *
         * 2,字符串中是否包含另一个字符串。
         *         boolean contains(string);
         *         int indexOf(string)//如果返回-1,表示不存在。
         *
         * 3,字符串中另一个字符串出现的位置。
         *         int indexOf(string)
         * 4,将字符串中指定的字符串替换成另一个字符串。
         *         String replace(oldstring , newstring)
         *
         * 5,字符串如何比较大小?
         *
         * 6,将字符串转成一个字符数组。或者字节数组。
         *         toCharArray()
         *         getBytes()
         * 7,将字母字符串转成大写的字母字符串。
         *         toUpperCase()
         *         toLowerCase();
         * 8,将字符串按照指定的方式分解成多个字符串, "lisi,wangwu,zhaoliu"获取三个姓名。
         *         String[] split(string);

#compareTo(对象)  对象比较方法

############查找一个字符串里面有多少个另外一个字符串

 1 public class StringTest2_2 {
 2     public static void main(String[] args) {
 3         /*
 4          * 案例二:
 5          * "witcasteritcasttyuiitcastodfghjitcast"有几个itcast
 6          * 
 7          * 思路:
 8          * 1,无非就是在一个字符串中查找另一个字符串。indexOf。
 9          * 2,查找到第一次出现的指定字符串后,如何查找第二个呢?
10          * 3,无需在从头开始,只要从第一次出现的位置+要找的字符串的长度的位置开始向后查找下一个第一次出现的位置即可。
11          * 4,当返回的位置是-1时,查找结束。
12          */
13         String str = "witcasteritcasttyuiitcastodfghjitcast";
14         String key = "itcast";
15         
16         int count = getKeyCount(str,key);
17         System.out.println("count="+count);
18         /*
19         int x = str.indexOf(key,0);//从头开始找。
20         System.out.println("x="+x);
21         
22         int y = str.indexOf(key,x+key.length());//从指定起始位开始找。
23         System.out.println("y="+y);
24         
25         int z = str.indexOf(key,y+key.length());//从指定起始位开始找。
26         System.out.println("z="+z);
27         
28         int a = str.indexOf(key,z+key.length());//从指定起始位开始找。
29         System.out.println("a="+a);
30         
31         int b = str.indexOf(key,a+key.length());//从指定起始位开始找。
32         System.out.println("b="+b);
33         */
34     }
35 
36     /**
37      * 获取key在str中出现次数。
38      */
39     public static int getKeyCount(String str, String key) {
40         
41         //1,定义变量。记录每一次找到的key的位置。
42         int index = 0;
43         //2,定义变量,记录出现的次数。
44         int count = 0;
45         
46         //3,定义循环。只要索引到的位置不是-1,继续查找。
47         while((index = str.indexOf(key,index))!=-1){
48             
49             //每循环一次,就要明确下一次查找的起始位置。
50             index = index + key.length();
51             
52             //每查找一次,count自增。
53             count++;
54         }
55         return count;
56     }
57 
58 }
View Code

##################另外一个程序:要求,将该字符串按照长度由长到短打印出来

public class StringTest2_3 {
    public static void main(String[] args) {
        /*
         * 案例三: "itcast_sh"要求,将该字符串按照长度由长到短打印出来。 itcast_sh itcast_s tcast_sh
         */
        String str = "itcast";
        printStringByLength(str);
    }
    public static void printStringByLength(String str) {
        // 1,通过分析,发现是for嵌套循环。
        for (int i = 0; i < str.length(); i++) {
            for (int start = 0, end = str.length() - i; end <= str.length(); start++, end++) {
                //根据start,end截取字符串。
                String temp = str.substring(start, end);
                System.out.println(temp);
            }
        }
    }
}
View Code



原文地址:https://www.cnblogs.com/-nbloser/p/8670430.html