String

作者: tangpy | 来源:发表于2017-09-11 22:23 被阅读0次

    1、获取
    1.1 字符串中的包含的字符数,也就是字符串的长度。
    int length(): 获取长度。
    1.2 根据位置获取位置上的某个字符。
    char charAt(int index):
    1.3 根据字符获取该字符在字符串中位置。
    int indexof(int ch): 返回的是ch在字符串中第一次出现的位置
    int indexof(int ch, int fromIndex):从fromIndex指定位置开始,获取ch在字符中出现的位置
    int indexof(String str): 返回的是str在字符串中第一次出现的位置
    int indexof(String str, int fromIndex):从fromIndex指定位置开始,获取str在字符中出现的位置
    int lastIndexOf(int ch) 返回指定字符的最后一次出现的字符串中的索引。

    2、判断:
    2.1 字符串中是否包含某一个子串。
    boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时才返回true。
    特殊之处:indexOf(str): 可以索引str第一次出现的位置,如果返回-1,表示该str不在字符串中。
    所以,也可用于指定判断是否包含。
    if(str.indexOf("aa") != -1)
    而且该方法即可以判断,也可以获取出现的位置。
    2.2 字符串中是否有内容。
    boolean isEmpty(): 原来就是判断长度是够为0.
    2.3 字符串中是否是以指定内容开头。
    boolean startsWith(String prefix)
    2.4 字符串中是否是以指定内容结尾。
    boolean endsWith(String prefix)
    2.5 判断字符串内容是否相同。复写了Object类中的equals方法。
    boolean equals(str);
    2.6 判断内容是否相同,并忽略大小写。
    boolean equalsIgnoreCase();

    3、转换
    3.1 将字符数组转成字符串。
    构造函数:String(char[])
    String(char[],offset,count): 将字符数组中的一部分转成字符串。
    静态方法:
    static String copyValueOf(char[])
    static String copyValueOf(char[] data, int offset, int count)
    static String valueOf(char[])
    3.2 将字符串转成字符数组。
    char[] toCharArray():
    3.3 将字节数组转成字符串。
    String(byte[])
    String(byte[],offset,count): 将字节数组中的一部分转成字符串。
    3.4 将字符串转成字节数组。
    byte[] getBytes():
    3.5 将基本数据类型转成字符串。
    static String valueOf(int)
    static String valueOf(double)
    特殊:字符串和字节数组在转换过程中,是可以指定编码表的。

    4、替换
    String replace(oldchar, newchar) 如果要替换的字符不存在,返回的还是原串。

    5、切割
    String[] split(regex);

    6、子串。获取字符串中的一部分。
    String substring(begin); 从指定位置开始到结尾。如果角标不存在,会出现字符串角标越界异常。
    String substring(begin, end); 包含头,不包含尾。 s.subString(0, s.length());

    7、转换,去除空格,比较。
    7.1 将字符串转成大写或者小写。
    String toUpperCase();
    String toLowerCase();
    7.2 将字符串两端的多个空格去除。
    String trim();
    7.3 对两个字符串进行自然顺序的比较。
    int compareTo(String);

    public class StringMethodDemo {
        
        public static void method_7() {
            String s = "   Hello Java   ";
            sop(s.toLowerCase());
            sop(s.toUpperCase());
            sop(s.trim());
            
            String s1 = "abc";
            String s2 = "aaa";
            sop(s1.compareTo(s2));
            
            
        }
        
        public static void method_substring() {
            String s = "abcdef";
            sop(s.substring(2)); //从指定位置开始到结尾。如果角标不存在,会出现字符串角标越界异常。
            sop(s.substring(2, 4)); // 包含头,不包含尾。 s.subString(0, s.length());
        }
        
        public static void method_split() {
            String s = "zhangsan, lisi, wangwu";
            String[] arr = s.split(",");
            for (int i = 0; i < arr.length; i++) {
                sop(arr[i]);
            }
        }
        
        public static void method_replace() {
            String s = "hello java";
    //      String s1 = s.replace('a', 'n'); //如果要替换的字符不存在,返回的还是原串。
            String s1 = s.replace("java", "world");
            sop("s = " + s + "  ---  s1 = " + s1);
        }
        
        public static void method_trans() {
            
            char[] arr = {'a', 'b', 'c', 'd', 'e', 'f'};
            String s = new String(arr,1,3);
            sop("s = " + s);
            
            String s1 = "zxcvbnm";
            char[] chs = s1.toCharArray();
            for (int i = 0; i < chs.length; i++) {
                sop("ch = " + chs[i]);
            }
        }
        
        public static void method_is() {
            String str = "ArrayDemo.java";
            
            //判断文件名称是否是Array单词开头。
            sop(str.startsWith("Array"));
            
            //判断文件名称是否是.java的文件
            sop(str.endsWith(".java"));
            
            //判断文件中是否包含Demo
            sop(str.contains("Demo"));
        }
    
        public static void method_get() {
            String str = "abcdeakpf";
            //长度
            sop(str.length());
            
            //根据索引获取字符
            sop(str.charAt(4)); //当访问到字符串中不寻在的角标时,会发生StringIndexOutOfBoundsException.
            
            //根据字符获取索引
            sop(str.indexOf('m',3)); //如果没有找到,返回-1
            
            //反向索引一个字符出现位置。
            sop(str.lastIndexOf("a"));
        }
        
        
        
        public static void main(String[] args) {
            
            method_7();
            method_substring();
            method_split();
            method_replace();
            method_trans();
            method_get();
            method_is();
            
            String s1 = "abc";
            String s2 = new String("abc");
            
            String s3 = "abc";
            
            System.out.println(s1 == s2);
            System.out.println(s1 == s3); 
    
        }
                        
        public static void sop(Object obj) {
            System.out.println(obj);
        }
    
    }
    

    练习

    public class StringTestDemo {
    
        public static void sop(String str) {
            System.out.println(str);
        }
        public static void main(String[] args) {
            
    //      String s = "  asfds sfasd     ";
    //      sop("("+s+")");
    //      s = myTrim(s);
    //      sop("("+s+")");
            
    //      sop("(" + recerseString(s) + ")");
    //      sop("(" + recerseString(s, 4, 7) + ")");
            
            /*
            String s1 = "bcabcdefbchibcjk";
            sop("count" + getSubCount(s1, "bc"));
            sop("count" + getSubCount_2(s1, "bc"));
            //sop("count === " + s1.split("bc").length);  //切的方法不建议使用,会有误差  
            */
            
            String s1 = "abcwerthelloyuiodef";
            String s2 = "cvhellobnm";
            sop(getMaxSubString(s1, s2));
            
    
            
        }
        
        /*
         * 练习一: 去除字符串两端空格
         */
        public static String myTrim(String str) {
            int start = 0, end = str.length() - 1;
            while(start<=end && str.charAt(start)==' ')
                start++;
            while(start<=end && str.charAt(end)==' ')
                end--;
            return str.substring(start, end+1);
        }
        
        /*
         * 练习二:将字符串进行反转。将字符串中指定部分进行反转,“abcdefg”  “abfedcg”
         */
        public static String recerseString(String s, int start, int end) {
            //字符串变数组
            char[] chs = s.toCharArray();
            
            //反转数组。
            reverse(chs, start, end);
            
            //将数组变成字符串
            return new String(chs);
        }
        public static String recerseString(String s) {
            return recerseString(s, 0, s.length());
        }
        private static void reverse(char[] arr, int x, int y) {
            for(int start=x, end=y-1; start<end; start++,end--) {
                swap(arr, start, end);
            }
        }
        private static void swap(char[] arr, int x, int y) {
            char temp = arr[x];
            arr[x] = arr[y];
            arr[y] = temp;
        }
        
        
        /*
         * 练习三:获取一个字符串在另一个字符串中出现的次数。
         */
        public static int getSubCount(String str, String key) {
            int count = 0;
            int index = 0;
            while((index = str.indexOf(key)) != -1) {
                sop("str = " + str);
                str = str.substring(index + key.length());
                count++;
            }
            return count;
        }
        
        //练习三:方式二
        public static int getSubCount_2(String str, String key) {
            int count = 0;
            int index = 0;
            while((index = str.indexOf(key,index)) != -1) {
                sop("index = " + index);
                index = index + key.length();
                count++;
            }
            return count;
        }
        
        /*
         * 练习4:获取两个字符串中最大相同子串
         */
        public static String getMaxSubString(String s1, String s2) {
            
            String max = "", min = "";
            max = (s1.length() > s2.length()) ? s1 : s2;
            min = (max==s1)?s2:s1;
            sop("max = " + max +".....min + " + min);
            for (int x = 0; x < min.length(); x++) {
                for (int y = 0,z = min.length()-x; z!=min.length()+1; y++,z++) {
                    String temp = s2.substring(y, z);
    //              sop(temp);
                    if(max.contains(temp))
                        return temp;
                } 
            }
            
            return "";
        }
        
    
    }
    

    相关文章

      网友评论

          本文标题:String

          本文链接:https://www.haomeiwen.com/subject/cwhqsxtx.html