Demo17_API_String

package test08;

public class Demo17_API_String {

    public static void main(String[] args) {
        /*学习API中常用的对象
         *String类
         *1.字符串都是对象
         *2.一旦初始化,就不能更改(常量)
         *3.通过string类的构造函数可以知道,将字节数组或者字节符转成字符串
         * 
         * 
         * String(char[] value, int offset, int count) //将数组中的一部分数组值转化为字符串
                  分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
         * */

        System.out.println("----多个引用指向同一个字符串 ----");
        String str = "abcdeasdf";
           String str1 = "abcdeasdf";
           System.out.println(str==str1);//可共享(两个对象指向的同一个对象)

           System.out.println("----两个内容相同,创建方式不同的字符串 ----");
           String s1 = "abc";
           String s2 = new String("abc");//只要是new的就会创建新的内存空间
           //s1只有一个常对象,s2有两个对象(构造函数对象(堆里),常对象)
           System.out.println(s1==s2);//false
           System.out.println(s1.equals(s2));//true   “==”指的是两个对象的引用相同,而“equals()”指的是两个对象的值相等
//     因为string复写了equals方法,建立了字符串自己的判断相同的依据,通过字符对象的内容来判断的      
    /*String str ="abcde" 
     * 1.字符串就是一个对象,那么他的方法必定是围绕着操作这个对象而定义的
     * 2.字符串功能有哪些?
     *   2.1有多少个字符(int)
     *   int  str= str.length();
     *   类型       数组名 
     *   2.2字符的位置(索引)(int)
     *   int index = str.indexOf("a");
     *   2.3获取所需位置上的字符
     *   char ch = str.charAt(3);
     *   2.4获取部分字符串
     *   String s = str.substring(2, 4);//包含头不包含尾
     *   
     *   

     * */
           System.out.println("-------------------");
//           2.1有多少个字符(int)     
    System.out.println("length="+str.length());
    int len = str.length();
    System.out.println("length="+len);
    System.out.println("-------------------");
//  2.2字符的位置(索引)(int)
    int index = str.indexOf("a");//获取的是字符a第一次出现的角标
    System.out.println("index="+index);
    System.out.println("-------------------");
    int index1 = str.indexOf("a",index+1);//从指定位置开始查找
    System.out.println("index1="+index1);
    System.out.println("-------------------");
    int index2 = str.lastIndexOf('a');
    int index3 = str.lastIndexOf('z');//不存在返回-1
    System.out.println("index2="+index2);
    System.out.println("index3="+index3);
    System.out.println("-------------------");
//  获取指定位置上的字符
    char ch = str.charAt(3);
//  char ch1 = str.charAt(30);
    System.out.println("ch="+ch);
//  System.out.println("ch1="+ch1);//字符串角标越界StringIndexOutOfBoundsException
    System.out.println("-------------------");
    //获取部分字符串
    String s = str.substring(2, 4);//包含头不包含尾
    System.out.println("s="+s);

    /*
     *string方法查找练习
     *1.字符串是否以指定字符串开头(结尾)。boolean
     *startWith(String)     endsWith(String)
     *2.字符串中是否包含另一个字符串
     *
     *3.字符串中另一个字符串出现的位置
     *
     *4.将字符串中指定的字符串替换成另一个字符串
     *
     *5.字符串比较大小
     *
     *6.将字符串转成字符数组或者字节数组
     *
     *7.将字母字符转成大写的字母字符串
     *
     *8.将字符串按照指定的方式分解成字符串
     *
     **/
    System.out.println("-------------------");
    //1.字符串是否以指定字符串开头(结尾)。
    String arr = "Demo17_API_String";
    boolean b1 = arr.startsWith("Demo");
    boolean b2= arr.endsWith("Demo");
    System.out.println("b1="+b1);
    System.out.println("b2="+b2);
    System.out.println("-------------------");
    //2.字符串中是否包含另一个字符串
    boolean b3 = arr.contains("Demo");
//  用indexOf查询字符串,如果返回值是-1,表示字符串不存在int a = arr.indexOf("Demo");
    System.out.println("b3="+b3);
    System.out.println("-------------------");
    //3.字符串中另一个字符串出现的位置
    int a23 = arr.indexOf("emo");
    System.out.println("a23="+a23);
    System.out.println("-------------------");
    //4.将字符串中指定的字符串替换成另一个字符串string(old,new);
//  没有被替换内容时返回原字符串
    String s3 = arr.replace("Demo", "demo");
    System.out.println("s3="+s3);
    System.out.println("-------------------");
    //5.字符串比较大小(返回值int)
    int result = arr.compareTo("Demo17_API_String");
    int result1 = "bc".compareTo("ab");//主要想让对象具备比较大小的功能只需要实现comparable接口
    System.out.println("result="+result);
    System.out.println("result1="+result1);
    System.out.println("-------------------");
    //6.将字符串转成字符数组或者字节数组
    char[] chs = arr.toCharArray();
    byte[] bytes = arr.getBytes();
    System.out.println("chs="+chs);/////////////////////////////////
    System.out.println("bytes="+bytes);
    System.out.println("-------------------");
    //7.将字母字符转成大写的字母字符串
    String upperString = arr.toUpperCase();
    String lowerString = arr.toLowerCase();
    System.out.println("upperString="+upperString);
    System.out.println("lowerString="+lowerString);
    System.out.println("-------------------");
    //8.将字符串按照指定的方式分解成字符串
    String arr1 = "lisi,wangwu,chenliu,sdjhb";
    String[] na = arr1.split(",");
    for(int i=0 ; i<na.length ; i++ )
    {
        System.out.println(na[i]);
    }
    System.out.println("-------------------");
    //创建一个字符串类型的数组
//  要求从小到大排列
    String[] strs = {"abc","nba","cctv","bbb"};
    sortString(strs);
    printArray(strs);
    }
        /*字符串数组排序
         * for嵌套
         * 进行元素大小比较,满足条件进行置换
         *  */
    public static void sortString(String[] strs) {
        // TODO Auto-generated method stub
        for(int i=0 ; i<strs.length - 1 ; i++)
        {
            for (int j = i + 1 ; j < strs.length ; j++ )
            {
                if(strs[i].compareTo(strs[2])>0)//对象比较用方法compareTo  大于零表示strs[i]>strs[j]那么就进行交换位置
                swap(strs, i, j);
            }
        }
    }
private static void swap(String[] strs, int i, int j) {
    // TODO Auto-generated method stub
    String temp = strs[i] ;
    strs[i] = strs[j];
    strs[j] = temp ;
}
//打印字符串数组
private static void printArray(String[] strs) {
    for (int i = 0; i < strs.length; i++) {
        System.out.println(strs[i]+" ");
    }
}

}

猜你喜欢

转载自blog.csdn.net/mingxu_W/article/details/81839394