`

org.springframework.util.StringUtils 工具类的使用

    博客分类:
  • J2SE
阅读更多
StringUtils.hasLength(null) = false
StringUtils.hasLength("") = false
StringUtils.hasLength(" ") = true
StringUtils.hasLength("Hello") = true

   StringUtils.hasText(null) = false
   StringUtils.hasText("") = false
   StringUtils.hasText(" ") = false
   StringUtils.hasText("12345") = true
   StringUtils.hasText(" 12345 ") = true
//是否包含空白字符
StringUtils.containsWhitespace(null)=false
StringUtils.containsWhitespace("")=false
StringUtils.containsWhitespace("a")=false
StringUtils.containsWhitespace("abc")=false
StringUtils.containsWhitespace("abc")=false
StringUtils.containsWhitespace(" ")=true
StringUtils.containsWhitespace(" a")=true
StringUtils.containsWhitespace("abc ")=true
StringUtils.containsWhitespace("a b")=true
StringUtils.containsWhitespace("a  b")
StringUtils.trimWhitespace(null)=null;
StringUtils.trimWhitespace("")="";
StringUtils.trimWhitespace(" ")="";
StringUtils.trimWhitespace("\t")="";
StringUtils.trimWhitespace(" a")="a";
StringUtils.trimWhitespace("a ")="a";
StringUtils.trimWhitespace(" a ")="a";
StringUtils.trimWhitespace(" a b ")="a b";

StringUtils.trimLeadingWhitespace(null)=null;
StringUtils.trimLeadingWhitespace("")="";
StringUtils.trimLeadingWhitespace(" ")="";
StringUtils.trimLeadingWhitespace("\t")="";
StringUtils.trimLeadingWhitespace(" a")="a";
StringUtils.trimLeadingWhitespace("a ")="a ";
StringUtils.trimLeadingWhitespace(" a ")="a ";
StringUtils.trimLeadingWhitespace(" a b ")="a b "
StringUtils.trimLeadingWhitespace(" a b  c ")="a b  c "

StringUtils.trimTrailingWhitespace(null)=null;
StringUtils.trimTrailingWhitespace(" ")="";
StringUtils.trimTrailingWhitespace("\t")="";
StringUtils.trimTrailingWhitespace("a ")="a";
StringUtils.trimTrailingWhitespace(" a")=" a";
StringUtils.trimTrailingWhitespace(" a ")=" a";
StringUtils.trimTrailingWhitespace(" a b ")=" a b";
StringUtils.trimTrailingWhitespace(" a b  c ")=" a b  c";


StringUtils.trimAllWhitespace("")="";
StringUtils.trimAllWhitespace(" ")="";
StringUtils.trimAllWhitespace("\t")="";
StringUtils.trimAllWhitespace(" a")="a";
StringUtils.trimAllWhitespace("a ")="a";
StringUtils.trimAllWhitespace(" a ")="a";
StringUtils.trimAllWhitespace(" a b ")="ab";
StringUtils.trimAllWhitespace(" a b  c "="abc";
//统计一个子字符串在字符串出现的次数
StringUtils.countOccurrencesOf(null, null) == 0;
StringUtils.countOccurrencesOf("s", null) == 0;
StringUtils.countOccurrencesOf(null, "s") == 0;
StringUtils.countOccurrencesOf("erowoiueoiur", "WERWER") == 0;
StringUtils.countOccurrencesOf("erowoiueoiur", "x")=0;
StringUtils.countOccurrencesOf("erowoiueoiur", " ") == 0;
StringUtils.countOccurrencesOf("erowoiueoiur", "") == 0;
StringUtils.countOccurrencesOf("erowoiueoiur", "e") == 2;
StringUtils.countOccurrencesOf("erowoiueoiur", "oi") == 2;
StringUtils.countOccurrencesOf("erowoiueoiur", "oiu") == 2;
StringUtils.countOccurrencesOf("erowoiueoiur", "oiur") == 1;
StringUtils.countOccurrencesOf("erowoiueoiur", "r") == 2;

//字符串替换
String inString = "a6AazAaa77abaa";
String oldPattern = "aa";
String newPattern = "foo";
// Simple replace
String s = StringUtils.replace(inString, oldPattern, newPattern);
s.equals("a6AazAfoo77abfoo")=true;

// Non match: no change
s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern);
s.equals(inString)=true
s = StringUtils.replace(inString, oldPattern, null);
s.equals(inString)=true

// Null old pattern: should ignore
s = StringUtils.replace(inString, null, newPattern);
        s.equals(inString)=true
//删除字符串

String inString = "The quick brown fox jumped over the lazy dog";
String noThe = StringUtils.delete(inString, "the");
noThe.equals("The quick brown fox jumped over  lazy dog")=true;
String nohe = StringUtils.delete(inString, "he");
nohe.equals("T quick brown fox jumped over t lazy dog")=true;
String nosp = StringUtils.delete(inString, " ");
nosp.equals("Thequickbrownfoxjumpedoverthelazydog")=true;
String killEnd = StringUtils.delete(inString, "dog");
killEnd.equals("The quick brown fox jumped over the lazy ")=true;
String mismatch = StringUtils.delete(inString, "dxxcxcxog");
  mismatch.equals(inString)=true;

//删除任何字符
//源代码如下
//char c = inString.charAt(i);
//如果不存在 c 值,则返回 -1
//if (charsToDelete.indexOf(c) == -1) {
//out.append(c);
//}

String inString = "Able was I ere I saw Elba";

String res = StringUtils.deleteAny(inString, "I");
        res.equals("Able was  ere  saw Elba")=true;
res = StringUtils.deleteAny(inString, "AeEba!");
res.equals("l ws I r I sw l")=true;
String mismatch = StringUtils.deleteAny(inString, "#@$#$^");
mismatch.equals(inString)=true;

//源代码如下 return (str != null ? "'" + str + "'" : null);
assertEquals("'myString'", StringUtils.quote("myString"));
assertEquals("''", StringUtils.quote(""));
assertNull(StringUtils.quote(null));
//将第一个字符改大写
StringUtils.capitalize(Str)
//将第一个个字符改小写
StringUtils.uncapitalize(str)

//mypath/myfile.txt" -> "myfile.txt
//获取字符串文件名和扩展名
StringUtils.getFilename("myfile").equals("myfile")=true;
StringUtils.getFilename("mypath/myfile".equals("myfile")=true;
StringUtils.getFilename("mypath/myfile".equals("myfile")=true;
StringUtils.getFilename("myfile.txt").equals("myfile.txt")=true;
StringUtils.getFilename("mypath/myfile.txt").equals("myfile.txt")=true;
//获取字符串扩展名,以.分隔
StringUtils.getFilenameExtension("myfile")=null;
StringUtils.getFilenameExtension("myPath/myfile")=null;
StringUtils.getFilenameExtension("myfile.").equals("")=true;
StringUtils.getFilenameExtension("myPath/myfile.").equals("")=true;
StringUtils.StringUtils.getFilenameExtension("myfile.txt").equals("txt")=true;
StringUtils.getFilenameExtension("mypath/myfile.txt").equals("txt")=true;

//舍去文件名扩展名
StringUtils.stripFilenameExtension(null)=true;
StringUtils.stripFilenameExtension("").equals("")=true;
StringUtils.stripFilenameExtension("myfile").equals("myfile")=true;
StringUtils.stripFilenameExtension("mypath/myfile").equals("mypath/myfile")=true;
StringUtils.stripFilenameExtension("myfile.").equals("myfile")=true;
StringUtils.stripFilenameExtension("mypath/myfile.").equals("mypath/myfile")=true;
StringUtils.stripFilenameExtension("mypath/myfile.").equals("mypath/myfile")=true;
StringUtils.stripFilenameExtension("myfile.txt").equals("myfile")=true;
StringUtils.stripFilenameExtension("mypath/myfile.txt").equals("mypath/myfile")=true
分享到:
评论

相关推荐

    java常用工具类整理

    1. org.springframework.util.StringUtils 工具类用于判断字符串非空,常用的方法有 isEmpty()、hasText() 等。 2. org.springframework.util.CollectionUtils 工具类用于判断 List 和 Map 集合的非空,常用的方法有...

    SpringBoot之自带工具类常用示例

    3. **`org.springframework.util.StringUtils`** 这是Spring提供的一个用于处理字符串的工具类,包含了许多实用方法,如`isEmpty()`, `isBlank()`, `join()`, `split()`等。例如,`StringUtils.isEmpty("str")`可以...

    Spring项目application.xml配置文件加解密

    import org.springframework.util.StringUtils; public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { @Override protected String resolvePlaceholder(String ...

    java常用工具类

    Java的`java.lang.String`类虽然已经提供了很多字符串操作的方法,但在实际开发中,`java.util.StringUtils`(来自Apache Commons Lang库)和`org.springframework.util.StringUtils`(Spring框架)等工具类提供了...

    Java中判断对象是否为空的方法的详解

    在 Java 中,有两个常用的 StringUtils 工具类可以用来判断对象是否为空,一是 org.apache.commons.lang3 包下的 StringUtils,另一个是 org.springframework.util 包下的 StringUtils。两者的参数类型不同,org....

    最全的 Java常用工具类.zip

    Spring Framework中的`org.springframework.util`包则包含了一些通用的工具方法,如`ObjectUtils`和`CollectionUtils`。 在实际项目开发中,这些工具类的使用可以极大地简化代码,例如,使用`StringUtils.isEmpty()...

    如何使用Spring工具类动态匹配url

    import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; public class UrlMatch { private UrlMatch() { } / * 匹配资料 * * @param patternPath 模糊匹配表达式 ...

    JAVA导出excel文件2003版,结合具体框架的实现

    import org.springframework.beans.factory.annotation.Autowired; import com.jerehsoft.common.SystemConfig; import com.jerehsoft.common.attach.AttachHelper; import com.jerehsoft.common.io.ReadFileHelper;...

    常用工具类方法1

    2. **org.springframework.util.StringUtils** - `hasText`: 检查字符串是否包含文本,排除null和全空格。 - `hasLength`: 判断字符串长度是否大于0。 - `isEmpty`: 检查字符串或对象是否为空,对null值进行了...

    定时任务cron 解析为中文.docx

    6. `org.springframework.util.Assert`:Spring Framework提供的断言工具类。 `CronExpParserUtil`的核心方法是`translateToChinese`,它接受一个cron表达式字符串作为输入,并返回该表达式的中文解释。在方法中,...

    git,工具類

    在 Spring 源码中,我们可以找到很多这样的工具类,如 `org.springframework.util.StringUtils`,它提供了许多实用的方法来处理字符串,如判断空值、格式化和比较等。 其次,Gradle 是一个构建自动化工具,它可以...

    Java数据传输之消息字符串应用

    可以使用`org.springframework.util.HtmlUtils`进行HTML转义,避免XSS攻击。同时,为了提高性能,可以使用`StringBuilder`或`StringBuffer`而非字符串连接操作。 六、总结 Java数据传输中的消息字符串应用涵盖了...

    16 个有用的的Java工具类(小结)

    七、org.springframework.util.StringUtils * hasText:检查字符串中是否包含文本 * hasLength:检测字符串是否长度大于 0 * isEmpty:检测字符串是否为空(若传入为对象,则判断对象是否为 null) * ...

    如何使用BeanUtils.copyProperties进行对象之间的属性赋值

    `BeanUtils.copyProperties`是Spring框架中的一个静态方法,位于`org.springframework.beans.BeanUtils`类中。它的主要功能是将源对象(source)的所有可读属性值复制到目标对象(target)。如果源对象和目标对象有...

    如何基于JAVA读取yml配置文件指定key内容

    import org.springframework.util.ResourceUtils; import org.yaml.snakeyaml.Yaml; import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.Set; public class YmlUtils { private...

    gateway和jwt网关认证实现过程解析

    import org.springframework.util.StringUtils; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; public class JwtUtil { public static final String SECRET = "qazwsx123444$#%#()*&&...

    JavaUtils:经常在项目中使用

    JavaUtils 通常作为一个开源库存在,比如 Apache Commons Lang 或者 Spring Framework 中的 `org.springframework.util` 包。这样的工具集大大提高了开发者的生产力,降低了代码的复杂度,使得开发过程更加顺畅。在...

Global site tag (gtag.js) - Google Analytics