栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java中String包含的所有方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中String包含的所有方法

public class Test03 {
    public static void main(String[] args) {
        String a = "HelloWorld! HelloChina";
        String b = "HelloChina!";
        String c = "HElloChina!";
        String d = "Hello";
        String f = "China!";
        char[] g = new char[a.length()];
        String h = "   123   " ;
        // charAt(index i)返回字符串中下标为 i 的字符 返回值:char类型
        System.out.println(a.charAt(4));
        // codePoitAt(index i) 返回字符串中下标为 i的字符 ASCII 码值 返回值:int类型
        System.out.println(a.codePointAt(4));
        // codePointBefore(index i) 返回字符串中下标为 i-1的字符 ASCII 码值 返回值:int类型
        System.out.println(a.codePointBefore(5));
        
        System.out.println(a.compareTo(b));
        // compareToIgnoreCase(String anotherString) 与 compareTo方法类似,可以忽视字符中的大小写 返回值:int类型
        System.out.println(a.compareToIgnoreCase(c));
        // concat(String anotherString) 将 anotherString 接到前面部分的字符串后面 返回值:String类型
        System.out.println(a.concat(b));
        
        System.out.println(a.contains(b));
        // endsWith(String anotherString) 判断前面字符串是否以 anotherString 字符串结尾 返回值 Boolean
        System.out.println(b.endsWith(f));
        // startsWith(String anotherString) 判断前面字符串是否以 anotherString 字符串开始 返回值 Boolean
        System.out.println(b.startsWith(f));
        // equals(Object object) 判断两个字符串是否相等 返回值 Boolean
        System.out.println(a.equals(a));
        // equalsIgnoreCase(String anotherString) 判断两个字符串是否相等,忽略大小写 返回值 Boolean
        System.out.println(a.equalsIgnoreCase(a.toUpperCase()));
        // 静态方法 String.format(Locale l, String format, Object... args)  没太搞懂,后面再进行补充
        System.out.println(String.format("Hi,%s %s %s", "我", "是个", "大帅哥"));
        
        a.getChars(0,9,g,0);
        System.out.println(g);
        // indexOf(String otherString) 返回 otherString第一次出现在前面字符串中位置的下标 返回值 int
        System.out.println(a.indexOf(d));
        // intern() 底层 c++程序调用的方法,直接返回字符常量池中已有的字符的地址 返回值 String
        String s1 = "hello" ;
        String s2 = new String("hello");
        System.out.println(s1 == s2);
        System.out.println(s1 == s2.intern());
        // isEmpty() 判断字符串是否为空,而不能判断是否为 null
        System.out.println(s1.isEmpty());
        System.out.println("".isEmpty());
        // lastIndexOf(String otherString) 返回 otherString最后一次出现在前面字符串中位置的下标 返回值  int
        System.out.println(a.lastIndexOf(d));
        // length() 输出字符串的长度 返回值 int
        System.out.println(a.length());
        // replaceAll(String oldString, String newString) 用新的字符串代替旧的字符串 返回值 String
        System.out.println(d.replaceAll(d,a));
        
        String s3 = "ah,b,c,d,e,f,g" ;
        String[] s4 = s3.split(",",-1);
        for (String s : s4) {
            System.out.print(s);
        }
        // substring(int beginIndex, int endIndex) 将字符串从 beginIndex开始到 endIndex-1 结束分割出来 返回值 String
        System.out.println(a.substring(0,3));
        // toCharArray() 将字符串转换成字符数组 返回值 char[]
        char[] s5 = a.toCharArray();
        for (char s : s5) {
            System.out.print(s);
        }
        // toLowerCase() toUpperCase 将字符串转换成小写/大写 返回值 String
        System.out.println(a.toLowerCase());
        System.out.println(a.toUpperCase());
        // trim() 将字符串前后的所有空格删除
        System.out.println(h.trim());
        // 静态方法 valueOf() 可以将 obj long int float double char[] char boolean 所有类型的量作为参数传进去 返回值 String
        System.out.println(String.valueOf(s5));
        System.out.println(String.valueOf(s5) instanceof String);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322419.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号