ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java基础之StringUtils操作

2021-10-08 23:02:18  阅读:170  来源: 互联网

标签:java inString equals myfile 操作 null true StringUtils


SpringUtils操作字符串

对字符串来进行处理的操作

第一个:hasLength

	// 这里传递的参数是String的父类CharSequence,这两个都没有针对" "这种类型来做判断
	public static boolean hasLength(@Nullable CharSequence str) {
        return str != null && str.length() > 0;
    }

    public static boolean hasLength(@Nullable String str) {
        return str != null && !str.isEmpty();
    }

第二个:hasText

    public static boolean hasText(@Nullable CharSequence str) {
        return str != null && str.length() > 0 && containsText(str);
    }

    public static boolean hasText(@Nullable String str) {
        return str != null && !str.isEmpty() && containsText(str);
    }
	
	// 看一下私有方法,为公有的方法来提供对应的操作的
	// 只要判断里面是包含有效字符的就直接返回true了
    private static boolean containsText(CharSequence str) {
        int strLen = str.length();

        for(int i = 0; i < strLen; ++i) {
            // Character.isWhitespace如果是空白字符,则返回true;如果不是,那么返回false
            if (!Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }
  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,inString,equals,myfile,操作,null,true,StringUtils
来源: https://www.cnblogs.com/likeguang/p/15383309.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有