常用类--String类--String类的其他功能

String类的其他功能 

程序示例

/*
        替换功能
            String replace(char old,char new)
            String replace(String old,String new)
        去除字符串两边空格
            String trim()
        按字典顺序比较两个字符串
            int compareTo(String str)
            int compareToIgnoreCase(String str)
*/

public class StringDemo11 {
    public static void main(String[] args) {
        String s = "helloworldowodadadwowo";

        //String replace(char old,char new)
        //将新的字符替换字符中指定的所有字符,并返回新的字符串
        String s1 = s.replace('l', 'a');
        System.out.println(s1);
        System.out.println(s);

        System.out.println("******************************");
      
        //String replace(String old,String new)
        //将字符串中旧的小串用新的小串替换,返回一个新的字符串
        String s2 = s.replace("owo", "wow");
        System.out.println(s2);

        String s3 = s.replace("owo", "qwerdf");
        System.out.println(s3);

        //如果被替换的字符串不存在,返回的是原本的字符串
        String s4 = s.replace("qwer", "poiu");
        System.out.println(s4);

        System.out.println("***********************************");
      
        //String trim() 去除字符串两边的空格
        String s5 = " hello world ";
        System.out.println(s5);// hello world //两边的空格被去掉了,但是中间的空格没动
        System.out.println(s5.trim());//hello world

        System.out.println("***********************************");
      
        //int compareTo(String str)
        String s6 = "hello"; //h的ASCII码值104
        String s7 = "hello";
        String s8 = "abc";  //a的ASCII码值97
        String s9 = "qwe"; //q的ASCII码值113
        System.out.println(s6.compareTo(s7)); //0--:104-104
        System.out.println(s6.compareTo(s8)); //7--:104-97
        System.out.println(s6.compareTo(s9)); //-9--:104-113

        String s10 = "hel";
        System.out.println(s6.compareTo(s10)); //2
      
      //那为什么这个结果是2呢?
    }
}

//int compareTo(String str)源码分析

String s6 = "hello";
String s8 = "abc"; 

s6.compareTo(s8)

//该值用于字符存储。
private final char value[];

public int compareTo(String anotherString) {
    //anotherString -- s8
    int len1 = value.length; // 5
    int len2 = anotherString.value.length; //3
    int lim = Math.min(len1, len2); //3
    char v1[] = value;
    char v2[] = anotherString.value;

    int k = 0;
    while (k < lim) { //0,1,2,3
        char c1 = v1[k]; //h,e,l
        char c2 = v2[k]; //h,e,l
        if (c1 != c2) {
            return c1 - c2;
        }
        k++;
    }
    return len1 - len2; //5-3
}

/*
注:
compareto -- 按照字典排序比较两个字符串
总结:
若两串长度相等,从前往后比较两串的每个字符的ASCII值
若两串长度不等,比较两串的长度
*/

习题

/*
        字符串反转
            举例:键盘录入”abc”		输出结果:”cba”
        分析:
            1、导包并创建键盘录入对象
            2、创建一个空的字符串
            3、将字符串转换成字符数组
            4、倒着遍历,得到每一个字符
            4、将每次获取到的字符拼接
            5、输出
*/

import java.util.Scanner;

public class StringDemo12 {
    public static void main(String[] args) {
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个字符串:");
        //键盘输入一个字符串
        String s = sc.next();

        //定义一个空的字符串
        String s1 = "";

        //将输入的字符串转换成字符数组
        char[] chars = s.toCharArray();

        //倒着遍历,得到每一个字符
        for (int i = chars.length - 1; i >= 0; i--) {
            //将每次获取到的字符拼接
            s1 += chars[i];
        }
        System.out.println("倒序后的字符串为:" + s1);
    }
}
posted @ 2021-12-14 22:46  赤兔胭脂小吕布  阅读(40)  评论(0)    收藏  举报