StringUtils常用方法

下列方法都是基于org.apache.commons.lang3.3.9包下的(StringUtIils方法都是null安全的)

文档:
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

isEmpty : 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0(变体有:isNotEmpty、isAnyEmpty、isNoneEmpty、isAllEmpty)

public static boolean isEmpty(CharSequence cs) {
    return cs == null || cs.length() == 0;
}

isBlank : 判断某字符串是否为空或长度为0或由空白符(whitespace) 构成(变体:isNotBlank、isAnyBlank、isNoneBlank、isAllBlank)

public static boolean isBlank(CharSequence cs) {
    int strLen;
    if (cs != null && (strLen = cs.length()) != 0) {
        for(int i = 0; i < strLen; ++i) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }

        return true;
    } else {
        return true;
    }
}
public static boolean isAnyEmpty(CharSequence... css) {
    if (ArrayUtils.isEmpty(css)) {
        return false;
    } else {
        CharSequence[] var1 = css;
        int var2 = css.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            CharSequence cs = var1[var3];
            if (isEmpty(cs)) {
                return true;
            }
        }

        return false;
    }
}

trim : 去掉字符串两端的多余的空格 (变体:trimToNull、trimToEmpty)

public static String trim(String str) {
    return str == null ? null : str.trim();
}

stripStart : 去掉字符串前指定的字符内容(变体stripEnd、stripAll、strip)

StringUtils.stripStart("starfish","star");       //fish

substring : 截取指定位置的字符串 (变体:substringAfter、substringBefore、substringBeforeLast、substringBetween)

public static String substring(String str, int start)

left : 从左侧开始截取字符串,其实是substring(变体:right、mid)

public static String left(String str, int len)

split : 分割 指定字符串(变体:splitByWholeSeparator、splitPreserveAllTokens)

public static String[] split(String str, String separatorChars)

join: 将数组或集合以某拼接符拼接到一起形成新的字符串

public static String join(Object[] array, String separator)

replace : 将指定字符串的某个字符串修改为另一个值(变体:replaceIgnoreCase、replaceEach、replaceChars)

public static String replace(String text, String searchString, String replacement)

equals: 比较两个字符串是否相等,如果两个均为空则也认为相等。(变体: 不区分大小写equalsIgnoreCase)**

public static boolean equals(CharSequence cs1, CharSequence cs2)

contains : 检查字符串seq 是否包含字符串searchSeq(变体:不区分大小写containsIgnoreCase、containsWhitespace、containsAny、containsOnly、containsNone)

public static boolean contains(CharSequence seq, CharSequence searchSeq) 

indexOf : 返回字符 searchChar 在seq 中第一次出现的位置。如果str 为 null 或 “”,或者 searchChar 没有在 seq中出现则返回-1 (变体有ordinalIndexOf、lastIndexOf、indexOfAny)

public static int indexOf(CharSequence seq, CharSequence searchSeq)

upperCase : 小写字符串转大写(lowerCase转小写)

System.out.println(StringUtils.upperCase("starfish"));    //STARFISH

capitalize : 将字符串首字母大写(uncapitalize 首字母转小写、swapCase 大小写互相转换)

System.out.println(StringUtils.capitalize("starfiSh"));  //StarfiSh

System.out.println(StringUtils.uncapitalize("StarfiSh"));  //starfiSh

eg:

System.out.println(StringUtils.isEmpty("    "));       //false

System.out.println(StringUtils.isNotBlank("     "));    //false

System.out.println(StringUtils.trim("  starfis h  "));      //starfis h

System.out.println("trim:"+StringUtils.trim(" \b \t \n \f \r "));   //trim:

System.out.println("trim1:"+StringUtils.trim(" \n\tss \b"));   //trim1:ss

System.out.println("strip:::"+StringUtils.strip(" \b \t \n \f \r "));  //   strip:: (\b退格后 少了个:)

System.out.println("strip1:"+StringUtils.strip(" \n\tss \b"));   //strip1:ss

System.out.println(StringUtils.stripStart("starfish","star"));    //fish

System.out.println(StringUtils.indexOf("starfish","st"));    //0

System.out.println(StringUtils.compare("starfish","starfish"));   //0

System.out.println(StringUtils.contains("starfish","star"));   //true

System.out.println(StringUtils.remove("starfish","star"));  //fish

System.out.println(StringUtils.substring("starfish",4));   //fish

System.out.println(StringUtils.left("starfish",4));   //star

System.out.println(StringUtils.equals("star",null));          //false

System.out.println(StringUtils.join(Arrays.asList("star","fish")));   //[star, fish]

System.out.println(StringUtils.join(new String[]{"a","b"},","));

System.out.println(StringUtils.replace("staafish","aa","ar"));

for (String st : StringUtils.split("s.t.a.r",".")) {
    System.out.println(st);          //s   t   a   r
}

System.out.println(StringUtils.upperCase("Starfish"));   //STARFISH

System.out.println(StringUtils.capitalize("starfiSh"));  //StarfiSh

System.out.println(StringUtils.uncapitalize("StarfiSh"));  //starfiSh

System.out.println(StringUtils.isNumeric("12"));  //true
  • IsEmpty/IsBlank - checks if a String contains text
  • Trim/Strip - removes leading and trailing whitespace
  • Equals/Compare - compares two strings null-safe
  • startsWith - check if a String starts with a prefix null-safe
  • endsWith - check if a String ends with a suffix null-safe
  • IndexOf/LastIndexOf/Contains - null-safe index-of checks
  • IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
  • ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters
  • Substring/Left/Right/Mid - null-safe substring extractions
  • SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
  • Split/Join - splits a String into an array of substrings and vice versa
  • Remove/Delete - removes part of a String
  • Replace/Overlay - Searches a String and replaces one String with another
  • Chomp/Chop - removes the last part of a String
  • AppendIfMissing - appends a suffix to the end of the String if not present
  • PrependIfMissing - prepends a prefix to the start of the String if not present
  • LeftPad/RightPad/Center/Repeat - pads a String
  • UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
  • CountMatches - counts the number of occurrences of one String in another
  • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
  • DefaultString - protects against a null input String
  • Rotate - rotate (circular shift) a String
  • Reverse/ReverseDelimited - reverses a String
  • Abbreviate - abbreviates a string using ellipsis or another given String
  • Difference - compares Strings and reports on their differences
  • LevenshteinDistance - the number of changes needed to change one String into another
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值