一、length()
返回此字符串的长度

 public static void main4(String[] args) {
        //length()方法
        String r = "woyaojindachang";
        int length = r.length();
        System.out.println(length);
    }
这里length返回的是"woyaojindachang"的长度,应该是15个字符


二、equals
将此字符串与指定对象进行比较

public static void main(String[] args) {
        //equals方法
        String r = "woyaojindachang";
        if(r.equals("woyaojindachang")) {
            System.out.println("字符串相等");
        } else {
            System.out.println("字符串不同");
        }
    }
这里的equals返回值是boolean,如果相等返回true,否则返回false

三、charAt()
返回 char指定索引处的值

public static void main(String[] args) {
        //charAt
        String s = "woyaojindachang";
        char s1 = s.charAt(5);
        System.out.println(s1);
    }
charAt()返回指定处的值,从0开始,5处是j.

四、indexOf()
返回指定字符第一次出现的字符串内的索引

public static void main(String[] args) {
        //indexOf
        String s = "woyaojindachang";
        int location = s.indexOf("j");
        System.out.println(location);
    }
这里返回的是j第一次出现的位置,从0开始,返回5

五、trim()
返回一个字符串,其值为此字符串,并删除任何前导和尾随空格

public static void main(String[] args) {
        //trim
        String s = " wo ";
        String s1 = s.trim();
        System.out.println(s1);
    }
trim去掉wo前面的空格和后面的空格.

六、compareTo()
按字典顺序比较两个字符串

public static void main(String[] args) {
        //compareTo
        String s = "woyaojindacahng";
        int s1 = s.compareTo("woyao");
        System.out.println(s1);
    }
若调用该方法的字符串大于参数字符串,则返回大于0的值, 若相等,则返回数0, 若小于参数字符串,则返回小于0的值

七、toLowerCase()
将字符串中的所有字符都转换为小写字符

  public static void main(String[] args) {
        //toLowerCase
        String s = "WOYAOJINDACHANG";
        String s1 = s.toLowerCase();
        System.out.println(s1);
    }

八、toUpperCase()
将字符串中的所有字符都转换为大写字符

public static void main(String[] args) {
        //toUpperCase
        String s = "woyaojindachang";
        String s1 = s.toUpperCase();
        System.out.println(s1);
    }

九、replace()
将此字符串与指定对象进行比较

public static void main(String[] args) {
        //replace的使用
        System.out.println("将日期中的-替换为.");
        String date = "2022-07-30";
        System.out.println("替换前: " date);
        String replace = date.replace("-",".");
        System.out.println("替换后: " replace);
    }
将2022-07-30中的-全部换成.

十、substring(int beginIndex)
返回字符串中从beginIndex开始的子串

public static void main(String[] args) {
        //substring
        String s = "woyaojindachang";
        String s1 = s.substring(5);
        System.out.println(s1);
    }
截取从第五位(j)开始的字符串

十一、substring(int beginIndex, int endIndex)
返回从beginIndex开始到endIndex-1的子串

public static void main(String[] args) {
        //substring字符串截取
        String testDate = "20220730";
        String year = testDate.substring(0,4);
        System.out.println(year);
        String month = testDate.substring(4,6);
        System.out.println(month);
        String day = testDate.substring(6,8);
        System.out.println(day);
        System.out.println(year "年" month "月" day "日");
    }
输入一个日期,分别截取年月日

总结
今天向大家介绍了String类的一些常用方法,大家可以去使用一下
以上就是Java中String类常用方法使用详解的详细内容,更多关于Java String类的资料请关注Devmax其它相关文章!