`
mengzhiang
  • 浏览: 23497 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

org.springframework.util.StringUtils的使用

阅读更多

我们经常会对字符串进行操作,spring已经实现了常用的处理功能。我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串。
工具类整理如下:
  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.PropertyPlaceholderHelper; import org.springframework.util.StringUtils; public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { @...

    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;...

    SpringSecurity Jwt Token 自动刷新的实现

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; ...

    springboot全局日期格式化的两种方式

    import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util....

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

    可以看到,StringUtils.isEmpty 方法能够判断对象是否为空,並且org.springframework.util 包下的 StringUtils 能够判断更多类型的对象是否为空。 判断数组是否为空 在 Java 中,判断数组是否为空可以使用 list....

    java基于spring注解AOP的异常处理的方法

    import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.spring...

    SpringBoot整合Elasticsearch7.2.0的实现方法

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework...

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

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

    spring boot配置拦截器代码实例

    import org.springframework.util.StringUtils; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import simple.proj.zxz.play....

    常用工具类方法1

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

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

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

    Java StringUtils字符串分割转数组的实现

    而使用 org.springframework.util.StringUtils 中的 delimitedListToStringArray(str, delimiter) 方法可以避免这种问题。该方法可以直接使用指定的分割字符进行分割,不需要进行转义。这使得字符串的分割变得更加...

    最全的 Java常用工具类.zip

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

    java常用工具类

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

    SpringMVC自定义类型转换器实现解析

    import org.springframework.util.StringUtils; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StingToDateConvertr ...

    Spring多种加载Bean方式解析

    Spring多种加载Bean方式...因此,怎么把xml标签解析为BeanDefinition(),入口是在org.springframework.beans.factory.xml.XmlBeanDefinitionReader这个类,但是实际干活的是在org.springframework.beans.factory.xml....

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

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

Global site tag (gtag.js) - Google Analytics