Day8:String

介绍String只是因为String在Java使用中是最频繁的工具类之一,写这个也只是为练习怎么去查API

    /*
         * String演示。
         *  "abcd"
         *  1,常量,一旦初始化就不会被改变。
         */
        String str = "abcd";
//        str = "hello";
//        String str = new String("abcd");
        
        System.out.println(str==str);//false
        System.out.println(str.equals(str));//true 字符串的equals覆盖了Object类,比较的是字符串的内容是否相同。
        
        //问,str和str1的区别。
        /*
         * str在内存中只有一个对象。
         * str1,在内存中有两个对象。
         */
        
        
        System.out.println("---------------");
        
        String s1 = "abc";
        String s2 = "abc";
        
        System.out.println(s1==s2);
/*
         * "abcd"
         * 
         * 查阅api发现 String类构造函数可以将字节数组,或者字符数组构造成字符串的对象。
         * 
         * 1,长度。
         *         int length()
         * 
         * 2,获取指定位置字符。
         *         char (int )
         * 
         * 3,获取字符所处的位置。
         *         int (char);
         * 
         */
        
        String str = "abcda";
        
        int len = str.length();
        
        char ch = str.charAt(0);
        
        int index = str.indexOf('k');//0
        int lastIndex = str.lastIndexOf('a');//4
package cn.itcast.string.test;

public class StringTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        /*
         * 不是鱼,是渔。 1,字符个数。 int length();
         * 
         * 2,把字符串分解很多字符 char[] toCharArray();
         * 
         * 3,对字符串中的字符排序。字符串中没有该方法。自定义。
         * 
         * 
         * 4,通过字符串中指定位置获取的对应的字符。 char charAt(int index);
         * 
         * 5,通过字符串中指定字符获取的第一次出现的位置 int indexOf(int ch);
         * 
         * 6,指定的字符串在原字符串中是否出现以及出现的位置 int indexOf(String str);
         * 
         * 7,字符串是否以指定的字符串开头. 结尾,是否包含指定字符串。 boolean startsWith(String) boolean
         * endsWith(String); boolean contains(String);
         * 
         * 8,获取字符串中的一部分--子串。 String substring(int start,int end);
         * 
         * 9,将字符串中的指定字符修改为另一个字符。"abc" "kbc"; String replace(char ch1,char ch2)
         * 
         * 10,去除字符串两端的空白,"  ab c  " "ab c" String trim();
         * 
         * 11,字符串可以比较大小吗?如果有!,将字符串数组排序。 记住:基本类型数值可以通过比较运算符比较大小和相等。> < ==
         * 对象也可以比较是否相等,谁大谁小。都是通过方法完成。 对象比较相同:Object类中 boolean equals(Object
         * obj):子类一般情况下都会复写,建立自己判断相同的依据。 对象比大小用的也是方法 该功能有三种情况。所以使用int类型。正数 负数 零.
         * 前者大于后者 返回正数, 前者小于后者返回负数 前者等于后者返回零。
         */
        // int num = "abc".compareTo("bcd");

        // int[] arr = {32,15,22,78,45};

        String[] strs = { "haha", "nba", "abc", "cba", "haha", "qq", "hiahia" };

        printArray(strs);

        // 对字符串数组排序。
        sort(strs);
        printArray(strs);

    }

    public static void sort(String[] strs) {

        for (int x = 0; x < strs.length - 1; x++) {
            for (int y = x + 1; y < strs.length; y++) {

                if (strs[x].compareTo(strs[y]) > 0) {
                    swap(strs, x, y);
                }
            }
        }

    }

    private static void swap(String[] strs, int x, int y) {
        String temp = strs[x];
        strs[x] = strs[y];
        strs[y] = temp;
    }

    public static void printArray(String[] strs) {
        for (int i = 0; i < strs.length; i++) {
            if (i != strs.length - 1)
                System.out.print(strs[i] + ",");
            else
                System.out.println(strs[i]);
        }
    }

}
package cn.itcast.string.test;

public class StringTest2 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String s1 = "hello";
        String s2 = "java";
        
        test(s1,s2);
        
        System.out.println(s1+"...."+s2);//hello ......  java
        
    }
    
    public static void test(String s1,String s2){
        s2.replace('a','o');
        s1 = s2;
    }
}
package cn.itcast.string.test;

public class StringTest3 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        /*
         * 需求3:子串在整串中出现的次数。"nbadfnbaghjnbaklnba"
         * 
         * 思路:
         * 1,需要计数。
         * 2,找到一个nba就计数。
         * 3,咋找?那就是字符串中查找字符串,字符串中怎么找应该字符串自己很清楚。
         * 所以找String类。
         * 4,如果有这个方法,每找一次需要计数,需要找n次。循环完成。
         * 
         * 步骤;
         * 1,定义变量,计数。
         * 2,需要循环,循环条件是,找到了就继续循环,没有找到就停。
         * int indexOf(String)
         * 3,循环内对计数器自增。
         */
        
        String str ="qqnbadfnbaghjnbaknbalnba";
        String key = "nba";
        
        int count = getKeyCount(str,key);
        System.out.println("count="+count);
        
    }

    public static int getKeyCount(String str, String key) {
        
        //1,定义变量计数。
        int count = 0;
        //2,定义变量,记录每次找到的角标。
        int index = 0;
        //2,循环。条件是indexOf查找的方法返回的结果不是-1.而且要明确下次查找的位置。indexOf(String,fromIndex);
        while((index = str.indexOf(key,index))!=-1){
            
            count++;
            //每找完一次,都要确定下次要找的起始位置。上次位置+key的长度。
            index += key.length();
            
        }
        return count;
    }

}
package cn.itcast.string.test;

public class StringTest4 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        /*
         * 需求4:两个字符串的最大相同子串。
         * "sadfcctvghjkl"
         * "cctvzxcv"
         * 
         * 思路:
         * 1,以短的字符串为主。
         *    到长的字符串中去判断是否存在,如果存在,已找到。
         * 2,如果没有找到。将短的字符串的长度递减获取子串继续到长的串中查找。只要找到就结束。
         * 3,没有找到,说明没有相同的。
         * 
         */
        
        String s1 = "sadfcctvghjkl";
        String s2 = "zxcctvcv";
        String maxSub = getMaxSubString(s2,s1);
        
        System.out.println("maxsub="+maxSub);
        
    }

    public static String getMaxSubString(String s1, String s2) {
        //确定哪个是长的哪个是短的。
        String longStr,shortStr;
        longStr = s1.length()>s2.length()?s1:s2;
        shortStr = s1.equals(longStr)?s2:s1;
        
//        System.out.println("long:"+longStr);
//        System.out.println("short:"+shortStr);
        //对短的字符串操作,从短串中取子串,到长字符串中判断,是否存在。
        for(int x=0; x<shortStr.length(); x++){
            
            
            for(int y=0,z=shortStr.length()-x; z<=shortStr.length(); y++,z++){
                
                //根据y,z,获取子串。
                String temp = shortStr.substring(y,z);
//                System.out.println(temp);
                if(longStr.contains(temp))
                    return temp;
                
            }
        }
        
        
        return null;
    }

}
package cn.itcast.string.test;

import java.util.Arrays;

public class StringTest5 {

    

    /**
      * @param args
     */
    public static void main(String[] args) {

        /*
         * 需求5:对字符串中字符进行自然顺序排序。
         * 思路:
         * 1,要排序,我会!但是只会数组排序。
         * 2,怎么能把字符串转成数组呢?
         * 3,到字符串中的找方法。
         * 4,排序我熟。
         * 5,将排序后的数组变成字符串
         * 
         */
        
        String str = "jbdsakncv";
        
        String sortString = sortChar(str);
        System.out.println(sortString);
    }
    /**
     * 对给定的字符串进行字符排序。并返回排序后字符串。
     * @param str
     * @return
     */
    public static String sortChar(String str) {
        
        //1,将字符串转成字符数组。
        char[] chs = stringToArray(str);
        
        //2,对数组排序。
        sort(chs);
        
        //3,将数组转成字符串。
        return toString(chs);
    }
    /*
     * 将字符数组转成字符串。
     */
    private static String toString(char[] chs) {
        
        return new String(chs);
    }
    /*
     * 对字符数组进行升序排序。
     */
    private static void sort(char[] chs) {
        
        Arrays.sort(chs);
        
    }
    /*
     * 将字符串转成字符数组。
     */
    private static char[] stringToArray(String str) {
        
        return str.toCharArray();
    }

}
原文地址:https://www.cnblogs.com/vijay/p/3508057.html