StringUtils工具类

本文介绍了Apache Commons Lang3中的StringUtils工具类,包括它的两个包版本及其区别,重点讲解了推荐使用的org.apache.commons.lang3.StringUtils,以及这个工具类中常用的处理CharSequence的方法。
摘要由CSDN通过智能技术生成

1、首先,当需要导包的时候,你需要了解StringUtils有两个包的可以选择,一个是org.apache.commons.lang3.StringUtils(推荐),另外一个是org.apache.commons.lang.StringUtils(已过时)。

2、这两者最大的区别就是两者支持的类型不一样。org.apache.commons.lang3.StringUtils大部分支持的类型是CharSequence,而org.apache.commons.lang.StringUtils是String。

3.现今apache也是推荐使用:org.apache.commons.lang3.StringUtils

//这是CharSequence借口支持的类型
public interface CharSequence {

    
    int length();

    char charAt(int index);

    public String toString();

    public default IntStream chars() {
        class CharIterator implements PrimitiveIterator.OfInt {
            int cur = 0;

            public boolean hasNext() {
                return cur < length();
            }

            public int nextInt() {
                if (hasNext()) {
                    return charAt(cur++);
                } else {
                    throw new NoSuchElementException();
                }
            }

            @Override
            public void forEachRemaining(IntConsumer block) {
                for (; cur < length(); cur++) {
                    block.accept(charAt(cur));
                }
            }
        }

        return StreamSupport.intStream(() ->
                Spliterators.spliterator(
                        new CharIterator(),
                        length(),
                        Spliterator.ORDERED),
                Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED,
                false);
    }

    public default IntStream codePoints() {
        class CodePointIterator implements PrimitiveIterator.OfInt {
            int cur = 0;

            @Override
            public void forEachRemaining(IntConsumer block) {
                final int length = length();
                int i = cur;
                try {
                    while (i < length) {
                        char c1 = charAt(i++);
                        if (!Character.isHighSurrogate(c1) || i >= length) {
                            block.accept(c1);
                        } else {
                            char c2 = charAt(i);
                            if (Character.isLowSurrogate(c2)) {
                                i++;
                                block.accept(Character.toCodePoint(c1, c2));
                            } else {
                                block.accept(c1);
                            }
                        }
                    }
                } finally {
                    cur = i;
                }
            }

            public boolean hasNext() {
                return cur < length();
            }

            public int nextInt() {
                final int length = length();

                if (cur >= length) {
                    throw new NoSuchElementException();
                }
                char c1 = charAt(cur++);
                if (Character.isHighSurrogate(c1) && cur < length) {
                    char c2 = charAt(cur);
                    if (Character.isLowSurrogate(c2)) {
                        cur++;
                        return Character.toCodePoint(c1, c2);
                    }
                }
                return c1;
            }
        }

        return StreamSupport.intStream(() ->
                Spliterators.spliteratorUnknownSize(
                        new CodePointIterator(),
                        Spliterator.ORDERED),
                Spliterator.ORDERED,
                false);
    }
}

//首先导入Maven依赖, 3.10是最新版
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.10</version>
</dependency>

介绍一下StringUtils常用的方法

//实例
//StringUtils.isBlank(null)      = true
//StringUtils.isBlank("")        = true
//StringUtils.isBlank(" ")       = true
//StringUtils.isBlank("bob")     = false
//StringUtils.isBlank(" bob ")   = false
public static boolean isBlank(CharSequence cs) {
        int strLen = length(cs);
        if (strLen == 0) {
            return true;
        } else {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        }
    }
//实例
//StringUtils.isEmpty(null)      = true
//StringUtils.isEmpty("")        = true
//StringUtils.isEmpty(" ")       = false
//StringUtils.isEmpty("bob")     = false
//StringUtils.isEmpty(" bob ")   = false
public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
}
//实例
//StringUtils.equals("","")           = true
//StringUtils.equals(null,null)       = true
//StringUtils.equals(null,"abc")      = false
//StringUtils.equals("abc",null)      = false
//StringUtils.equals("abc","abc")     = true
//StringUtils.equals("abc","ABC")     = false
public static boolean equals(CharSequence cs1, CharSequence cs2) {
        if (cs1 == cs2) {
            return true;
        } else if (cs1 != null && cs2 != null) {
            if (cs1.length() != cs2.length()) {
                return false;
            } else if (cs1 instanceof String && cs2 instanceof String) {
                return cs1.equals(cs2);
            } else {
                int length = cs1.length();

                for(int i = 0; i < length; ++i) {
                    if (cs1.charAt(i) != cs2.charAt(i)) {
                        return false;
                    }
                }

                return true;
            }
        } else {
            return false;
        }
    }
//实例
//StringUtils.equalsIgnoreCase("","")           = true
//StringUtils.equalsIgnoreCase(null,null)       = true
//StringUtils.equalsIgnoreCase(null,"abc")      = false
//StringUtils.equalsIgnoreCase("abc",null)      = false
//StringUtils.equalsIgnoreCase("abc","abc")     = true
//StringUtils.equalsIgnoreCase("abc","ABC")     = true
public static boolean equalsIgnoreCase(CharSequence cs1, CharSequence cs2) {
        if (cs1 == cs2) {
            return true;
        } else if (cs1 != null && cs2 != null) {
            return cs1.length() != cs2.length() ? false : CharSequenceUtils.regionMatches(cs1, true, 0, cs2, 0, cs1.length());
        } else {
            return false;
        }
    }
//实例:substring是从0开始计算的
//StringUtils.substring("abc",0,2) = "ab"
public static String substring(String str, int start, int end) {
        if (str == null) {
            return null;
        } else {
            if (end < 0) {
                end += str.length();
            }

            if (start < 0) {
                start += str.length();
            }

            if (end > str.length()) {
                end = str.length();
            }

            if (start > end) {
                return "";
            } else {
                if (start < 0) {
                    start = 0;
                }

                if (end < 0) {
                    end = 0;
                }

                return str.substring(start, end);
            }
        }
    }

推荐使用!!!(导包的时候看清楚哦):org.apache.commons.lang3.StringUtils

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值