【实例】Java常用的String Api

本文详细探讨了Java中String类的常用API,包括创建、操作和比较字符串的方法。通过实例解析,帮助开发者更好地理解和应用这些API,提升编程效率。
摘要由CSDN通过智能技术生成
package com.aran.String;

/**
 * @Author Aran
 * @Date 2020/3/17 9:43 下午
 * 常用的String的Api
 */
public class StringApiTest {
    public static void main(String[] args) {
        String testStr = "Everyday is a happy day!";

        // 返回给定位置的代码单元
        char aChar = testStr.charAt(9);
        //i
        System.out.println(aChar);

        /*
        * 返回从给定位置开始的码点
        * */
        int aCodePoint = testStr.codePointAt(10);
        //115
        System.out.println(aCodePoint);

        /*
        * 返回从startIndex码点开始,cpCount个码点后的码点索引
        * */
        int offIndex = testStr.offsetByCodePoints(11,5);
        System.out.println(offIndex);

        /*
        * 按照字典顺序,如果字符串位于other之前,返回一个负数,之后,则返回整数,相等,则返回0
        * */
        String testStr2 = "Tomorrow is another today";
        int comResult = testStr.compareTo(testStr2);
        System.out.println(comResult);

        /*
        * 将这个字符串的码点作为一个流返回,调用toArray将它们放在一个数组中
        * */
        int[] codeP = testStr.codePoints().toArray();
        System.out.println(codeP[0]);

        /*
        * 用数组中从offset开始的count个码点构造一个字符串
        * */
        String newStr = new String(codeP,2,10);
        System.out.println(newStr);

        /*
        * 字符串为空判断
        * */
        boolean isEmpty = testStr.isEmpty();
        System.out.println(isEmpty);

        /*
        * 字符串由空格组成的判断(From JDK11)
        * */
        boolean isBlank = testStr.isBlank();
        System.out.println(isBlank);

        /*
        * 判断字符串相等
        * */
        boolean judgeEqual = testStr.equals(testStr2);
        System.out.println(judgeEqual);

        /*
        * 判断字符串相等(忽略大小写)
        * */
        boolean judgeEqualIgnoreCase = "".equalsIgnoreCase(testStr);
        System.out.println(judgeEqualIgnoreCase);

        /*
        * 判断字符串是否以指定的字符串开头或结尾
        * */
        boolean isStartStr = testStr.startsWith("E");
        boolean isEndStr = testStr.endsWith("!");
        System.out.println(isStartStr);
        System.out.println(isEndStr);

        /*
        * 返回与字符串testStr或码点cp匹配的第一个子串的开始位置,从索引0或fromIndex开始匹配。如果在原始字符串中不存在str,则返回-1
        * */
        int indexOne = testStr.indexOf("day");
        int indexTwo = testStr.indexOf("h",3);
        int indexThree = testStr.indexOf(69);
        int indexFour = testStr.indexOf(69,0);
        System.out.println(indexOne);
        System.out.println(indexTwo);
        System.out.println(indexThree);
        System.out.println(indexFour);

        /*
        * 返回与字符串testStr或码点cp匹配的最后一个子串的开始位置。从原始字符串末尾或者fromIndex开始匹配。
        * */
        int lastIndexOne = testStr.lastIndexOf("happy");
        int lastIndexTwo = testStr.lastIndexOf("day",2);
        int lastIndexThree = testStr.lastIndexOf(69);
        int lastIndexFour = testStr.lastIndexOf(115,3);
        System.out.println(lastIndexOne);
        System.out.println(lastIndexTwo);
        System.out.println(lastIndexThree);
        System.out.println(lastIndexFour);

        /*
        * 返回字符串代码单元的个数
        * */
        int cpCounter = testStr.length();
        System.out.println(cpCounter);

        /*
        * 返回startIndex 和 endIndex-1之间的码点个数
        * */
        int cpAnotherCounter = testStr.codePointCount(0,10);
        System.out.println(cpAnotherCounter);

        /*
        * 返回一个新的字符串,这个字符串用newString代替原始字符串中所有的oldString,
        * 可以用String或StringBuilder对象作为CharSequence参数
        * */
        String aNewStr = testStr.replaceAll("happy","very happy");
        System.out.println(aNewStr);

        /*
        * 返回一个新字符串,这个字符串包含原始字符串中从beginIndex到字符串末尾或endIndex-1的所有代码单元
        * */
        String firstNewStr = testStr.substring(3);
        String secondNewStr = testStr.substring(3,10);
        System.out.println(firstNewStr);
        System.out.println(secondNewStr);

        /*
        * 将字符串转变为大写或小写
        * */

        String lowerCaseStr = testStr.toLowerCase();
        String upperCaseStr = testStr.toUpperCase();
        System.out.println(lowerCaseStr);
        System.out.println(upperCaseStr);


        /*
        *
        * 返回一个新字符串。这个字符串将删除原始字符串头部和尾部小于等于U+0020的字符串(trim)或空格(strip)
        * */
        String trimStr = testStr.trim();
        String stripStr = testStr.strip();
        System.out.println(trimStr);
        System.out.println(stripStr);

        /*
        * 返回一个新字符串,用给定的分界符连接所有元素
        * */
        String joinStr = String.join(" | ","ha","heihei","hehe");
        System.out.println(joinStr);
        
        
        
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值