String类库的一些方法使用

String类:

charAt​(int index)返回指定数字下标的字符char(返回值类型char)注:从0开始
        String a = "abcdefg";
        char b = a.charAt(2);
        System.out.println(b);

chars()
空(IntStream)
        String a = "abcdefg";
        IntStream b = a.chars();
        System.out.println(b);

codePointAt ​(int index)

  返回指定数字下标字符的ASCLL码(int)注:从0开始 .的ASCLL码是46

        String a = "abcdefg.";
        int b = a.codePointAt(7);
        System.out.println(b);

codePointBefore​(int index)返回指定数字下标前一位字符的ASCLL码(int)注:从0开始 .的ASCLL码是46
        String a = "abcdefg.a";
        int b = a.codePointBefore(8);
        System.out.println(b);

codePointCount​(int beginIndex, int endIndex)文本范围的长度(在char s中)是endIndex-beginIndex ,就是后一个数字减去前一个数字(int)
        String a = "abcdefg.a";
        int b = a.codePointCount(1,7);
        System.out.println(b);

codePoints()空(IntStream)
        String a = "abcdefg.a";
        IntStream b = a.codePoints();
        System.out.println(b);

compareTo​(String anotherString)从左往右遍历前后字符串,前x位相同 ,x+1位不同则输出前后x+1位的差,前小于后输出负数,大于后输出正数;如果前=后输出0;如前“abd”,后“abcd",前后属于包含关系则输出:前者减去后者length的差(ASCLL之差)(int)
..
 String a = "abc";
        int b = a.compareTo("abcd");
        System.out.println(b);
        String c = "asd";
        int d = c.compareTo("zsdasdsa");
        System.out.println(d);

compareToIgnoreCase​(String str)使用方法和compareTo一样,忽略大小写所带的ASCLL的不同(int)
        String a = "ABC";
        int b = a.compareToIgnoreCase("ab");
        System.out.println(b);

concat​(String str)将指定的字符串连接到此字符串的末尾(String)
        String a = "abc";
        String b = a.concat("DEF");
        System.out.println(b);

contains​(CharSequence s)判断一段字符串是否有我们指定查找的字符串,如果有则ture,没有false(boolean)
        String a = "hjgabcdfji";
        boolean b = a.contains("abc");
        System.out.println(b);

contentEquals

(CharSequence cs)

判断cs字符串是否等于指定字符串,相等则返回ture,类似于eaulas重写(boolean)
        String a = "hjgabcdfji";
        boolean b = a.contentEquals("hjgabcdfji");
        System.out.println(b);

contentEquals​(StringBuffer sb)用于StringBuffer中比较用法和上面相同(boolean)
endsWith​(String suffix)判断字符串是否用指定的后缀结尾(boolean)
        String a = "abcde";
        boolean b = a.endsWith("de");
        System.out.println(b);

equals​(Object anObject)判断两个字符串是否相等。注:前者不可以为null否则会出现空指针问题(boolean)
        String a = "abcde";
        boolean b = a.equals("abcde");
        System.out.println(b);

equalsIgnoreCase​(String anotherString)忽略大小写的比较,用法和上面相同
getBytes()使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中。(byte [ ])
        String a = "abcde";
        byte[] b = a.getBytes();
        System.out.println(b);

getChars​(int srcBegin, int srcEnd, char[] dst, int dstBegin)将目标字符串从起始位置到结束位置复制到一个数组中,从指定的下标开始存储。(void)
        String s = "abcde";
        char[] array = new char[10];
        s.getChars(0,5,array,2);
        for(char a:array){//循环输出array
            System.out.println(a);

hashCode()返回此字符串的哈希码。(int)
        String s = "abcde";
        System.out.println(s.hashCode());

indexOf从一组字符串中查找另一组字符串出现的第一次的位置从0开始(int)
        String s = "abcde";
        System.out.println(s.indexOf("cd"));//返回指定字符串中出现的位置从0开始
        System.out.println(s.indexOf("cd",3));//从指定位置查找字符串

lastIndexOf从一组字符串中查找另一组字符串出现的最后一次的位置从0开始(int)
        String s = "abcdecdecd";
        System.out.println(s.lastIndexOf("cd"));//返回cd最后一次出现的位置从0开始

length()返回字符串的长度(int)
        String s = "abcdecdecd";
        System.out.println(s.length());//返回此字符串的长度

replace
将字符串替换为指定字符串,可以只替换第一次出现的位置,也可以全部替换(String)
        String s = "abcdabcdcd";
        System.out.println(s.replace("c","m"));//将字符串替换为指定字符串
        System.out.println(s.replaceAll("c","m"));
        System.out.println(s.replaceFirst("c","m"));//将第一次出的目标字符串替换为指定字符串,其他不变

split​(String regex)将字符串按照指定字符分隔开(String[  ])
        String s = "i love java";
        String[] arr = s.split(" ",2);//将字符串按空格分开存储到数组中,limit限制分开的数量
        for (String a :arr){
            System.out.println(a);
        }

startsWith​(String prefix)
从指定位置字符串查看是否已指定的字符串为开头,是返回true,不是返回false(boolean)
        String s1 = "i love java";
        System.out.println(s1.startsWith("i l"));//字符串是否已指定的字符串为开头
        System.out.println(s1.startsWith("il"));
        System.out.println(s1.startsWith("java",7));//从指定位置字符串是否已指定的字符串为开头
        System.out.println(s1.startsWith("java",6));

strip() 返回一个去掉前后空格的字符串,或去掉前空格,或去掉最后空格的字符串(String)
        String s1 = " i love java ";
        System.out.println(s1.strip());//去掉前后空格
        System.out.println(s1.stripLeading());//去掉前空格
        System.out.println(s1.stripTrailing());//去掉后空格

toCharArray()将此字符串转换为新的字符数组。

返回类型为char[  ]

 public static void main(String[] args) {
        String a = "abcdefg";
        char[] b = a.toCharArray();
        for (int i = 0; i < b.length; i++) {
            System.out.println("第"+i+"个字符为"+b[i]);
        }
    }

toUpperCase()将此字符串所有字母转换为大写
toLowerCase()将此字符串所有字母转换为小写
        String s1 = "i lOvE jAva123";
        System.out.println(s1.toUpperCase(Locale.ROOT));//转换大写
        System.out.println(s1.toLowerCase());//转换为小写

substring​(int beginIndex, int endIndex)从指定位置返回一个子字符串(String)
        String s1 = "i lOvE jAva123";
        System.out.println(s1.substring(5,8));//从5到8返回一个子字符串

toString()返回一个字符串描述(String)
        String a = "abcde";
        System.out.println(a.toString());

trim()返回一个去掉前面和后面的空格的字符串,空格被定义为代码点小于或等于 'U+0020' (空格字符)的任何字符。 
        String a = "       abcde  ";
        System.out.println(a.trim());

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值