String API方法的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014338530/article/details/87934111
@Test
    public void contextLoads() {
        String str = "SpringbootStringapiApplicationTests";
        /** charAt(),返回指定位置的char值 */
        System.out.println(str.charAt(2));//r
        /** compareTo,比较两个字符串,返回值是整型,它是先比较对应字符的大小(ASCII码顺序),如果第一个字符和参数的第一个字符不等,结束比较,
            返回他们之间的差值,如果第一个字符和参数的第一个字符相等,
            则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方。
            a.compareTo(b)
            如果a等于b,则返回值 0;
            如果a小于b,则返回一个大于 0 的值;
            如果a大于b,则返回一个小于 0 的值。
        */
        String s = "boot";
        System.out.println(s.compareTo(str));//15
        /** compareToIgnoreCase()
         *  方法用于按字典顺序比较两个字符串,不考虑大小写。
            a.compareToIgnoreCase(b)
            如果a等于b,则返回值 0;
            如果a小于b,则返回一个小于 0 的值;
            如果a大于b,则返回一个大于 0 的值。
         */
        s = "SpringBOOTStringapiApplicationTests";
        System.out.println(str.compareToIgnoreCase(s));//0
        /** concat(),字符串拼接,等同于str = str + “XXX"; */
        System.out.println(str.concat(",这是一个测试类!"));//SpringbootStringapiApplicationTests,这是一个测试类!
        /** contentEquals(),用户string和stringbuffer比较
         *  如字符串与指定 StringBuffer 表示相同的字符序列,则返回 true;否则返回 false。
         */
        StringBuffer sb = new StringBuffer("SpringbootStringapiApplicationTests");
        System.out.println(str.contentEquals(sb));//true
        /** endsWith() 方法用于测试字符串是否以指定的后缀结束
            注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true。
         */
        System.out.println(str.endsWith("Tests"));//true
        System.out.println(str.endsWith("Test"));//false
        System.out.println(str.endsWith(""));//true
        /** equals(),判断是否相等*/
        System.out.println(str.equals("SpringbootStringapiApplicationTests"));//true
        /** equalsIgnoreCase(),判断是否相等,忽略大小写*/
        System.out.println(str.equalsIgnoreCase("SpringBOOTStringapiApplicationTests"));
        /** getBytes(String charsetName),指定的字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中
         *  getBytes(), 使用平台的默认字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
         *  byte[] 数组中存放的是字符串响应位置对应的字母的ASCII码
         */
        String string = "hello2";
        byte[] bytes = string.getBytes();
        System.out.println(Arrays.toString(bytes));//[104, 101, 108, 108, 111, 50]
        /** getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin),方法将字符从字符串复制到目标字符数组。
         * srcBegin -- 字符串中要复制的第一个字符的索引。
         * srcEnd -- 字符串中要复制的最后一个字符之后的索引。
         * dst -- 目标数组。
         * bdstBegin -- 目标数组中的起始偏移量。
         */
        char[] chars = new char[10];
        str.getChars(0,chars.length,chars,0);
        System.out.println(Arrays.toString(chars));//[S, p, r, i, n, g, b, o, o, t]
        /**hashCode() 方法用于返回字符串的哈希码。*/
        System.out.println(str.hashCode());//-658552997
        /** indexOf() 方法有以下四种形式:
            indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。ch为ASCII码
            indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。ch为ASCII码
            indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
            indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
         */
        System.out.println(str.indexOf("b"));//6
        System.out.println(str.indexOf("a",10));//16
        System.out.println(str.indexOf(99));//99是c 24
        System.out.println(str.indexOf(97,10));//97是a 16
        /** lastIndexOf() 方法有以下四种形式:
         lastIndexOf(int ch): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。ch为ASCII码
         lastIndexOf(int ch, int fromIndex): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。ch为ASCII码
         lastIndexOf(String str): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
         lastIndexOf(String str, int fromIndex): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
         */
        System.out.println(str.lastIndexOf("b"));//6
        System.out.println(str.lastIndexOf("s",10));//-1
        System.out.println(str.lastIndexOf(99));//99是c 24
        System.out.println(str.lastIndexOf(97,10));//97是a -1
        /** matches() 方法用于检测字符串是否匹配给定的正则表达式。
         *  Pattern.matches(regex, str)
         */
        System.out.println(str.matches("XXX"));//false
        /**
         * regionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len)
         * 如果字符串的指定子区域匹配字符串参数的指定子区域,则返回 true;否则返回 false。是否完全匹配或考虑大小写取决于 ignoreCase 参数。
         *      ignoreCase -- 如果为 true,则比较字符时忽略大小写。
         *      toffset -- 此字符串中子区域的起始偏移量。
         *      other -- 字符串参数。
         *      ooffset -- 字符串参数中子区域的起始偏移量。
         *      len -- 要比较的字符数。
         *
         */
        s = "boot";
        System.out.println(str.regionMatches(0,s,0,str.length()));//false
        System.out.println(str.regionMatches(6,s,0,4));//true
        /** replace(char oldChar, char newChar) 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。
         *  oldChar -- 原字符。
         *  newChar -- 新字符。
         */
        String replace = s.replace('b', 'B');
        System.out.println(replace);//Boot
        /** startsWith() 方法用于检测字符串是否以指定的前缀开始。
         *  startsWith(String prefix, int toffset)
         *  startsWith(String prefix)
         *  prefix -- 前缀。
         *  toffset -- 字符串中开始查找的位置。
         */
        System.out.println(str.startsWith("Sp"));//true
        System.out.println(str.startsWith("Sp",10));//false
        /** subSequence(int beginIndex, int endIndex) 方法返回一个新的字符序列,它是此序列的一个子序列。
         *  beginIndex -- 起始索引(包括)。
         *  endIndex -- 结束索引(不包括)。
         */
        System.out.println(str.subSequence(0,10));//Springboot
        /** substring() 方法
         *  substring(int beginIndex)
         *  substring(int beginIndex, int endIndex)
         *  beginIndex -- 起始索引(包括), 索引从 0 开始。
         *  endIndex -- 结束索引(不包括)。
         */
        System.out.println(str.substring(10));//StringapiApplicationTests
        System.out.println(str.substring(10,15));//Strin
        /**toCharArray() 方法将字符串转换为字符数组。 */
        System.out.println(Arrays.toString(s.toCharArray()));//[b, o, o, t]
        /** toLowerCase() 方法将字符串转换为小写。*/
        System.out.println("Boot".toLowerCase());//boot
        /** toUpperCase() 方法将字符串小写字符转换为大写。*/
        System.out.println("boot".toUpperCase());//BOOT
        /** trim() 方法用于删除字符串的头尾空白符。*/
        System.out.println(" boot ".trim());//boot
        /** valueOf(参数) 返回指定参数类型的字符串表示形式
         *  valueOf(boolean b): 返回 boolean 参数的字符串表示形式。.
         *  valueOf(char c): 返回 char 参数的字符串表示形式。
         *  valueOf(char[] data): 返回 char 数组参数的字符串表示形式。
         *  valueOf(char[] data, int offset, int count): 返回 char 数组参数的特定子数组的字符串表示形式。
         *  valueOf(double d): 返回 double 参数的字符串表示形式。
         *  valueOf(float f): 返回 float 参数的字符串表示形式。
         *  valueOf(int i): 返回 int 参数的字符串表示形式。
         *  valueOf(long l): 返回 long 参数的字符串表示形式。
         *  valueOf(Object obj): 返回 Object 参数的字符串表示形式。
         */

        System.out.println(String.valueOf(true));//true
        System.out.println(String.valueOf('C'));//C
        System.out.println(String.valueOf(new char[]{'r', 'u', 'n', 'o', 'o', 'b' }));//runoob
        System.out.println(String.valueOf(new char[]{'r', 'u', 'n', 'o', 'o', 'b' },0,5));//runoo
        System.out.println(String.valueOf(1000.00));//1000.0
        System.out.println(String.valueOf(1000.0));//1000.0
        System.out.println(String.valueOf(100));//100
    }

才疏学浅,有不对的地方还请指出,谢谢!

猜你喜欢

转载自blog.csdn.net/u014338530/article/details/87934111
今日推荐