返回顶部
首页 > 资讯 > 后端开发 > JAVA >HuTool_字符串工具-StrUtil
  • 580
分享到

HuTool_字符串工具-StrUtil

javaservlet开发语言 2023-08-17 11:08:04 580人浏览 八月长安
摘要

这个工具的用处类似于Apache Commons Lang中的StringUtil 常用的方法 String str = "abCDEfghi";//是否为空boolean blank = StrUtil.isBlank(str);//fa

这个工具的用处类似于Apache Commons Lang中的StringUtil

常用的方法

String str = "abCDEfghi";//是否为空boolean blank = StrUtil.isBlank(str);//false//是否不为空boolean notBlank = StrUtil.isNotBlank(str);//true//去掉字符串后缀(removeprefix:前缀)String removeSuffix = StrUtil.removeSuffix("test.txt", ".txt");//test//忽略大小写去掉前缀(removeSuffixIgnoreCase:去掉后缀)String removePrefixIgnoreCase = StrUtil.removePrefixIgnoreCase(str, "A");//bCDEfghi//sub方法//顺数第2个到第4个,包含尾部包含头String sub = StrUtil.sub(str, 2, 4);//CD//-3表示倒数第三个字符String sub1 = StrUtil.sub(str, 2, -3);//CDEf//fORMat方法(使用字符串模板代替字符串拼接)String template = "{}爱{}!{}";String fin = StrUtil.format(template, "我", "JAVA","哈"); //我爱JAVA!哈 

1. hasBlank、hasEmpty方法

就是给定一些字符串,如果一旦有空的就返回true,常用于判断好多字段是否有空的(例如WEB表单数据)。

这两个方法的区别是hasEmpty只判断是否为null或者空字符串(“”),hasBlank则会把不可见字符也算做空,isEmpty和isBlank同理。

sub方法

避免subString方法越界问题,index的位置还支持负数哦,-1表示最后一个字符(这个思想来自于python),还有就是如果不小心把第一个位置和第二个位置搞反了,也会自动修正(例如想截取第4个和第2个字符之间的部分也是可以的

public static String sub(CharSequence str, int fromIndex, int toIndex) public static String subPreGbk(CharSequence str, int len, CharSequence suffix) public static String maxLength(CharSequence string, int length) public static String subPre(CharSequence string, int toIndex) public static String subSuf(CharSequence string, int fromIndex) public static String subSufByLength(CharSequence string, int length) public static String subWithLength(String input, int fromIndex, int length) public static String subBefore(CharSequence string, CharSequence separator, boolean isLastSeparator) public static String subBefore(CharSequence string, char separator, boolean isLastSeparator) public static String subAfter(CharSequence string, CharSequence separator, boolean isLastSeparator) public static String subAfter(CharSequence string, char separator, boolean isLastSeparator) public static String subBetween(CharSequence str, CharSequence before, CharSequence after) public static String subBetween(CharSequence str, CharSequence beforeAndAfter)

String str = “abcdefgh”;
String strSub1 = StrUtil.sub(str, 2, 3); //strSub1 -> c
String strSub2 = StrUtil.sub(str, 2, -3); //strSub2 -> cde
String strSub3 = StrUtil.sub(str, 3, 2); //strSub2 -> c

去空格 回车操作 与空有关的方法

public static boolean isBlank(CharSequence str)
public static boolean isBlankIfStr(Object obj)
public static boolean isNotBlank(CharSequence str)
public static boolean hasBlank(CharSequence… strs)
public static boolean isAllBlank(CharSequence… strs)
public static boolean isEmpty(CharSequence str)
public static boolean isEmptyIfStr(Object obj)
public static boolean isNotEmpty(CharSequence str)
public static String nullToEmpty(CharSequence str)
public static String nullToDefault(CharSequence str, String defaultStr)
public static String emptyToDefault(CharSequence str, String defaultStr)
public static String blankToDefault(CharSequence str, String defaultStr)
public static String emptyToNull(CharSequence str)
public static boolean hasEmpty(CharSequence… strs)
public static boolean isAllEmpty(CharSequence… strs)
public static boolean isNullOrUndefined(CharSequence str)
public static boolean isEmptyOrUndefined(CharSequence str)
public static boolean isBlankOrUndefined(CharSequence str)
public static String cleanBlank(CharSequence str)

// 去空格

StrUtil.cleanBlank

// 去\n\r

StrUtil.removeAllLineBreaks()

字符串包含关系

// 字符串中: 同时匹配 “小明” 和 “19岁” 这两个字符才返回true

boolean isContain = StrUtil.containsAll(“我叫小明今年18岁职业java软件工程师”, “小明”, “19岁”);

public static boolean startWith(CharSequence str, char c) public static boolean startWith(CharSequence str, CharSequence prefix, boolean isIgnoreCase) public static boolean startWith(CharSequence str, CharSequence prefix) public static boolean startWithIgnoreCase(CharSequence str, CharSequence prefix) public static boolean startWithAny(CharSequence str, CharSequence... prefixes) public static boolean endWith(CharSequence str, char c) public static boolean endWith(CharSequence str, CharSequence suffix, boolean isIgnoreCase) public static boolean endWith(CharSequence str, CharSequence suffix) public static boolean endWithIgnoreCase(CharSequence str, CharSequence suffix) public static boolean endWithAny(CharSequence str, CharSequence... suffixes) public static boolean contains(CharSequence str, char searchChar) public static boolean containsAny(CharSequence str, CharSequence... testStrs) public static boolean containsAny(CharSequence str, char... testChars) public static boolean containsBlank(CharSequence str) public static String getContainsStr(CharSequence str, CharSequence... testStrs) public static boolean containsIgnoreCase(CharSequence str, CharSequence testStr) public static boolean containsAnyIgnoreCase(CharSequence str, CharSequence... testStrs) public static String getContainsStrIgnoreCase(CharSequence str, CharSequence... testStrs)

头尾的一些处理

public static String trim(CharSequence str) public static void trim(String[] strs) public static String trimToEmpty(CharSequence str) public static String trimToNull(CharSequence str) public static String trimStart(CharSequence str) public static String trimEnd(CharSequence str) public static String trim(CharSequence str, int mode) public static String strip(CharSequence str, CharSequence prefixOrSuffix) public static String strip(CharSequence str, CharSequence prefix, CharSequence suffix) public static String stripIgnoreCase(CharSequence str, CharSequence prefixOrSuffix) public static String stripIgnoreCase(CharSequence str, CharSequence prefix, CharSequence suffix) public static String addPrefixIfNot(CharSequence str, CharSequence prefix) public static String addSuffixIfNot(CharSequence str, CharSequence suffix) public static boolean isSurround(CharSequence str, CharSequence prefix, CharSequence suffix) public static boolean isSurround(CharSequence str, char prefix, char suffix) 

删除字符操作

public static String removeAll(CharSequence str, CharSequence strToRemove) public static String removeAll(CharSequence str, char... chars) public static String removeAllLineBreaks(CharSequence str) public static String removePreAndLowerFirst(CharSequence str, int preLength) public static String removePreAndLowerFirst(CharSequence str, CharSequence prefix) public static String removePrefix(CharSequence str, CharSequence prefix) public static String removePrefixIgnoreCase(CharSequence str, CharSequence prefix) public static String removeSuffix(CharSequence str, CharSequence suffix) public static String removeSufAndLowerFirst(CharSequence str, CharSequence suffix) public static String removeSuffixIgnoreCase(CharSequence str, CharSequence suffix) 

大小写的转换

public static String upperFirstAndAddPre(CharSequence str, String preString) public static String upperFirst(CharSequence str) public static String lowerFirst(CharSequence str) public static boolean isUpperCase(CharSequence str) public static boolean isLowerCase(CharSequence str) 

分割操作

public static String[] splitToArray(CharSequence str, char separator) public static long[] splitToLong(CharSequence str, char separator) public static long[] splitToLong(CharSequence str, CharSequence separator) public static int[] splitToInt(CharSequence str, char separator) public static int[] splitToInt(CharSequence str, CharSequence separator) public static List split(CharSequence str, char separator) public static String[] splitToArray(CharSequence str, char separator, int limit) public static List split(CharSequence str, char separator, int limit) public static List splitTrim(CharSequence str, char separator) public static List splitTrim(CharSequence str, CharSequence separator) public static List splitTrim(CharSequence str, char separator, int limit) public static List splitTrim(CharSequence str, CharSequence separator, int limit) public static List split(CharSequence str, char separator, boolean isTrim, boolean ignoreEmpty) public static List split(CharSequence str, char separator, int limit, boolean isTrim, boolean ignoreEmpty) public static List split(CharSequence str, CharSequence separator, int limit, boolean isTrim, boolean ignoreEmpty) public static String[] split(CharSequence str, CharSequence separator) public static String[] split(CharSequence str, int len) public static String[] cut(CharSequence str, int partLength) 

判断相等

public static boolean equals(CharSequence str1, CharSequence str2) public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2) public static boolean equals(CharSequence str1, CharSequence str2, boolean ignoreCase) public static boolean isSubEquals(CharSequence str1, int start1, CharSequence str2, int start2, int length, boolean ignoreCase) public static boolean isAllCharMatch(CharSequence value, Matcher matcher) public static boolean equalsCharAt(CharSequence str, int position, char c) 

出现次数统计

public static int count(CharSequence content, CharSequence strForSearch) public static int count(CharSequence content, char charForSearch)

来源地址:https://blog.csdn.net/liuerchong/article/details/129795331

--结束END--

本文标题: HuTool_字符串工具-StrUtil

本文链接: https://lsjlt.com/news/372814.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作