`

StringUtils

    博客分类:
  • Java
 
阅读更多

一:将数组中的字符转换为一个字符串

1、 将数组中的字符转换为一个字符串 

    String[3] s={"a","b","c"} 
    StringUtil.convString(s)="a,b,c" ;

2,static public String converString(String strToConv, String conv) 

   @param strToConv 要转换的字符串 
   @param conv 分隔符,默认以逗号分隔 

   String[3] s={"a","b","c"} 
   StringUtil.convString(s,"@")="a@b@c" 

二:空值处理

3,空值检测:

  public static boolean isEmpty(String str) 

  @param str the String to check, may be null 
  @return true if the String is empty or null 

  判断一个字符串是否为空,空格作非空处理。 

  StringUtils.isEmpty(null) = true ;StringUtils.isEmpty("") = true; StringUtils.isEmpty(" ") = false; 

  StringUtils.isEmpty("bob") = false ;StringUtils.isEmpty(" bob ") = false ;

4,非空处理: 

  public static boolean isNotEmpty(String str) 

  @param str the String to check, may be null 
  @return true if the String is not empty and not null 

  判断一个字符串是否非空,空格作非空处理. 

  StringUtils.isNotEmpty(null) = false; StringUtils.isNotEmpty("") = false ;StringUtils.isNotEmpty(" ") = true; 

  StringUtils.isNotEmpty("bob") = true ;StringUtils.isNotEmpty(" bob ") = true ;

5, 去空格处理 :返回去除空格后的对象,若是null,返回null

  public static String trim(String str) 

  @param str the String to be trimmed, may be null 
  @return the trimmed string, null if null String input 

  格式化一个字符串中的空格,有非空判断处理;

  StringUtils.trim(null) = null; StringUtils.trim("") = "" ;StringUtils.trim(" ") = ""; StringUtils.trim("abc") = "abc"; 

  StringUtils.trim(" abc ") = "abc" ;

6,去空格处理 :返回去除空格后的对象,若是null/""/" ",返回null

  public static String trimToNull(String str) 

  @param str the String to be trimmed, may be null 

  格式化一个字符串中的空格,有非空判断处理,如果为空返回null; 

  StringUtils.trimToNull(null) = null ;StringUtils.trimToNull("") = null ;StringUtils.trimToNull(" ") = null ;

  StringUtils.trimToNull("abc") = "abc";     StringUtils.trimToNull(" abc ") = "abc";

7,空格处理 :返回去除空格后的对象,若是null/""/" ",返回""

  public static String trimToEmpty(String str)  

  @param str the String to be trimmed, may be null 

  格式化一个字符串中的空格,有非空判断处理,如果为空返回"";

  StringUtils.trimToEmpty(null) = "" ;StringUtils.trimToEmpty("") = "" ;StringUtils.trimToEmpty(" ") = "" ;

  StringUtils.trimToEmpty("abc") = "abc" ;StringUtils.trimToEmpty(" abc ") = "abc"; 

三:字符串比较:

9,public static boolean equals(String str1, String str2) 

   判断两个字符串是否相等,有非空处理,关注大小写。 

   StringUtils.equals(null, null) = true; StringUtils.equals(null, "abc") = false; StringUtils.equals("abc", null) = false;

   StringUtils.equals("abc", "abc") = true ;StringUtils.equals("abc", "ABC") = false ;

10,public static boolean equalsIgnoreCase(String str1, String str2) 

    判断两个字符串是否相等,有非空处理。忽略大小写 

    StringUtils.equalsIgnoreCase(null, null) = true ;StringUtils.equalsIgnoreCase(null, "abc") = false ;

    StringUtils.equalsIgnoreCase("abc", null) = false ;StringUtils.equalsIgnoreCase("abc", "abc") = true ;

    StringUtils.equalsIgnoreCase("abc", "ABC") = true ;

四:IndexOf 处理 

11,public static int indexOf(String str, String searchStr) 

    @param str the String to check, may be null 
    @param searchStr the String to find, may be null 
    @return the first index of the search String

    返回要查找的字符串第一次所在位置,有非空处理 

    StringUtils.indexOf(null, *) = -1; StringUtils.indexOf(*, null) = -1 ;StringUtils.indexOf("", "") = 0 ;

    StringUtils.indexOf("aabaabaa", "a") = 0; StringUtils.indexOf("aabaabaa", "b") = 2 ;StringUtils.indexOf("aabaabaa", "ab") = 1;  

    StringUtils.indexOf("aabaabaa", "") = 0 ;

12,public static int indexOf(String str, String searchStr, int startPos) 

    @param str the String to check, may be null 
    @param searchStr the String to find, may be null 
    @param startPos the start position, negative treated as zero 
    @return the first index of the search String, -1 if no match or null string input 

    返回要由指定位置开始查找的字符串第一次所在位置,有非空处理 

    StringUtils.indexOf(null, *, *) = -1; StringUtils.indexOf(*, null, *) = -1 ;StringUtils.indexOf("", "", 0) = 0 ;

    StringUtils.indexOf("aabaabaa", "a", 0) = 0 ;StringUtils.indexOf("aabaabaa", "b", 0) = 2 ;StringUtils.indexOf("aabaabaa", "ab", 0) = 1;

    StringUtils.indexOf("aabaabaa", "b", 3) = 5; StringUtils.indexOf("aabaabaa", "b", 9) = -1; StringUtils.indexOf("aabaabaa", "b", -1) = 2;

    StringUtils.indexOf("aabaabaa", "", 2) = 2 ;StringUtils.indexOf("abc", "", 9) = 3 ;
五:子字符串处理

13,public static String substring(String str, int start) 

   @param str the String to get the substring from, may be null 
   @param start the position to start from, negative means 
      count back from the end of the String by this many characters 
   @return substring from start position, null if null String input 

   返回指定位置开始的字符串中的所有字符, start>0表示从左向右, start<0表示从右向左, start=0则从左第一位开始

   StringUtils.substring(null, *) = null ;StringUtils.substring("", *) = "" ;StringUtils.substring("abc", 0) = "abc";

   StringUtils.substring("abc", 2) = "c" ;StringUtils.substring("abc", 4) = "" ;StringUtils.substring("abc", -2) = "bc";

   StringUtils.substring("abc", -4) = "abc" ;

14,public static String substring(String str, int start, int end)

    @param str the String to get the substring from, may be null 
    @param start the position to start from, negative means 
      count back from the end of the String by this many characters 
    @param end the position to end at (exclusive), negative means 
       count back from the end of the String by this many characters 
    @return substring from start position to end positon, null if null String input 

    返回由开始位置到结束位置之间的子字符串, 

     start>0&&end>0从左开始(包括左)到右结束(不包括右),

     start<0&&end<0从右开始(包括右),再向左数到end结束(包括end)

    StringUtils.substring(null, *, *) = null ;StringUtils.substring("", * , *) = "";

    StringUtils.substring("abc", 0, 2) = "ab" ;StringUtils.substring("abc", 2, 0) = ""; StringUtils.substring("abc", 2, 4) = "c";

    StringUtils.substring("abc", 4, 6) = "" ;StringUtils.substring("abc", 2, 2) = ""; StringUtils.substring("abc", -2, -1) = "b";

    StringUtils.substring("abc", -4, 2) = "ab" ;
15,
public static String substringBefore(String str, String separator) 
    @param str the String to get a substring from, may be null 
    @param separator the String to search for, may be null 
    @return the substring before the first occurance of the separator, 
null if null String input 

    Gets the substring before the first occurance of a separator. 
    The separator is not returned. 
    A null string input will return null. 
    An empty ("") string input will return the empty string. 
    A null separator will return the input string. 
    返回指定字符串之前的所有字符 

   StringUtils.substringBefore(null, *) = null; StringUtils.substringBefore("", *) = ""; StringUtils.substringBefore("abc", "a") = "";

   StringUtils.substringBefore("abcba", "b") = "a"; StringUtils.substringBefore("abc", "c") = "ab";

   StringUtils.substringBefore("abc", "d") = "abc" ;StringUtils.substringBefore("abc", "") = "" ;

   StringUtils.substringBefore("abc", null) = "abc" ;

16,public static String substringAfter(String str, String separator) 

    @param str the String to get a substring from, may be null 
    @param separator the String to search for, may be null 
    @return the substring after the first occurance of the separator, null if null String input 

   Gets the substring after the first occurance of a separator. 
   The separator is not returned. 
   A null string input will return null. 
   An empty ("") string input will return the empty string. 
   A null separator will return the empty string if the input string is not null. 
   返回指定字符串之后的所有字符

   StringUtils.substringAfter(null, *) = null; StringUtils.substringAfter("", *) = "" ;StringUtils.substringAfter(*, null) = ""; 

   StringUtils.substringAfter("abc", "a") = "bc"; StringUtils.substringAfter("abcba", "b") = "cba";

   StringUtils.substringAfter("abc", "c") = ""; StringUtils.substringAfter("abc", "d") = "" ;StringUtils.substringAfter("abc", "") = "abc" ;

17,public static String substringBeforeLast(String str, String separator) 

   Gets the substring before the last occurance of a separator. 
   The separator is not returned. 
   A null string input will return null. 
   An empty ("") string input will return the empty string. 
   An empty or null separator will return the input string. 

   返回最后一个指定字符串之前的所有字符 

   StringUtils.substringBeforeLast(null, *) = null ;StringUtils.substringBeforeLast("", *) = "";

   StringUtils.substringBeforeLast("abcba", "b") = "abc" ;StringUtils.substringBeforeLast("abc", "c") = "ab";

   StringUtils.substringBeforeLast("a", "a") = ""; StringUtils.substringBeforeLast("a", "z") = "a";

   StringUtils.substringBeforeLast("a", null) = "a" ;StringUtils.substringBeforeLast("a", "") = "a"; 

18,public static String substringAfterLast(String str, String separator) 

    Gets the substring after the last occurance of a separator. The separator is not returned. 
    A null string input will return null. An empty ("") string input will return the empty string. 
    An empty or null separator will return the empty string if the input string is not null. 

    返回最后一个指定字符串之后的所有字符 

    StringUtils.substringAfterLast(null, *) = null; StringUtils.substringAfterLast("", *) = "" ;StringUtils.substringAfterLast(*, "") = "";  

    StringUtils.substringAfterLast(*, null) = "" ;StringUtils.substringAfterLast("abc", "a") = "bc" ;

    StringUtils.substringAfterLast("abcba", "b") = "a"; StringUtils.substringAfterLast("abc", "c") = "";

    StringUtils.substringAfterLast("a", "a") = "" ;StringUtils.substringAfterLast("a", "z") = "";

 

六: Replacing(字符串替换)

19,public static String replace(String text, String repl, String with) 

   @param text text to search and replace in, may be null 
   @param repl the String to search for, may be null 
   @param with the String to replace with, may be null 
   @return the text with any replacements processed, null if null String input 

   Replaces all occurances of a String within another String. A null reference passed to this method is a no-op. 

   以指定字符串替换原来字符串的的指定字符串 ,repl是所给字符串中存在的需要被替换的字符串,with是需要被替换成的新字符串;

   StringUtils.replace(null, *, *) = null; StringUtils.replace("", *, *) = ""; StringUtils.replace("aba", null, null) = "aba";

   StringUtils.replace("aba", null, null) = "aba"; StringUtils.replace("aba", "a", null) = "aba"; 

   StringUtils.replace("aba", "a", "") = "aba"; StringUtils.replace("aba", "a", "z") = "zbz" ;
20,public static String replace(String text, String repl, String with, int max) 

    @param text text to search and replace in, may be null 
    @param repl the String to search for, may be null 
    @param with the String to replace with, may be null 
    @param max maximum number of values to replace, or -1 if no maximum 
    @return the text with any replacements processed,null if null String input 

    Replaces a String with another String inside a larger String, for the first max values of the search String. 
    A null reference passed to this method is a no-op.

    以指定字符串最大替换原来字符串的的指定字符串,max>0从左向右到max处截止(包括max)替换0到max之间指定的repl为with 

    StringUtils.replace(null, *, *, *) = null; StringUtils.replace("", *, *, *) = ""; StringUtils.replace("abaa", null, null, 1) = "abaa";

    StringUtils.replace("abaa", null, null, 1) = "abaa"; StringUtils.replace("abaa", "a", null, 1) = "abaa" ;

    StringUtils.replace("abaa", "a", "", 1) = "abaa"; StringUtils.replace("abaa", "a", "z", 0) = "abaa" ;

    StringUtils.replace("abaa", "a", "z", 1) = "zbaa" ;StringUtils.replace("abaa", "a", "z", 2) = "zbza";

    StringUtils.replace("abaa", "a", "z", -1) = "zbzz" ;    

 

七,Case conversion(大小写转换) 

21,public static String upperCase(String str) 

    @param str the String to upper case, may be null 
    @return the upper cased String, null if null String input 

    Converts a String to upper case as per {@link String#toUpperCase()}. A null input String returns null. 

     将一个字符串变为大写

     StringUtils.upperCase(null) = null; StringUtils.upperCase("") = ""; StringUtils.upperCase("aBc") = "ABC" ;

22,public static String lowerCase(String str) 

    @param str the String to lower case, may be null 
    @return the lower cased String, null if null String input 

     A null input String returns null. 
     将一个字符串转换为小写 

     StringUtils.lowerCase(null) = null ;StringUtils.lowerCase("") = "" ;StringUtils.lowerCase("aBc") = "abc" ;

23,public static String capitalize(String str)

    将字符串中的首字母大写 

    StringUtils.capitalize(null) = null; StringUtils.capitalize("") = "" ;

    StringUtils.capitalize("cat") = "Cat" ;StringUtils.capitalize("cAt") = "CAt" ;

分享到:
评论

相关推荐

    StringUtils的各项用法

    《StringUtils的深度解析与应用》 在Java编程中,处理字符串是常见的任务,Apache Commons Lang库中的StringUtils类提供了丰富的字符串操作方法,极大地提高了开发效率。本文将深入探讨StringUtils的几个重要功能,...

    org.apache.commons.lang3.StringUtils.jar.rar

    在给定的标题 "org.apache.commons.lang3.StringUtils.jar.rar" 中,我们可以看到这个压缩包包含的是 `StringUtils.jar`,实际上它是一个 `common-lang3.jar` 文件的别名。这个 JAR 包是 Apache Commons Lang 项目的...

    org.apache.commons.lang3.StringUtils.jar

    java获取客户端ip(经过多次代理)提示StringUtils cannot be resolved 需要先 import org.apache.commons.lang3.StringUtils; /* 内含 common-lang3.jar commons-lang3-3.9-bin.zip commons-lang3-3.9-src.zip ...

    自定封装StringUtils常用方法

    继承了org.apache.commons.lang3.StringUtils工具类,加入了部分常用方法,使用时直接添加到项目的公共utils下,同时在pom.xml加入依赖: &lt;!-- ...

    StringUtils(最新)

    `StringUtils` 是 Apache Commons Lang 库中的一个核心类,提供了大量关于字符串操作的实用方法,旨在作为 Java 核心库的扩展。这个库在 `commons-lang-2.5` 版本中是最新的,尽管现在可能有更新的版本,但这个版本...

    stringUtils的jar包

    org.apache.commons.lang3.StringUtils的jar包,判断字符串为空

    StringUtils工具类的使用

    `StringUtils`工具类是Java开发中非常常用的一个类库,主要提供了一系列静态方法来处理字符串。这个工具类在处理字符串的常见操作时提供了很大的便利,比如数组转字符串、空值检测、非空处理以及空格处理等。接下来...

    StringUtils API 使用方法

    `StringUtils` API 是 Apache Commons Lang 库中的一个实用工具类,专门为处理 `java.lang.String` 对象提供了丰富的静态方法。这个库是对 Java 标准库中的 `String` 类方法的一个扩展,尤其在处理 `null` 和空白...

    StringUtils源码及使用文档

    `StringUtils`是Apache Commons Lang库中的一个核心工具类,它提供了大量的静态方法,用于处理字符串。这个类在Java开发中非常常见,因为它弥补了Java标准库中对字符串操作的不足。`StringUtils`类包含了多种字符串...

    StringUtils jar包

    `StringUtils` jar包是Java开发中的一个实用工具库,它为处理字符串提供了许多方便的方法。这个库主要由Apache Commons Lang项目提供,这是一个广泛使用的开源组件,旨在补充Java标准库中对于字符串操作的功能不足。...

    StringUtils (Lang 2_3 API)

    `StringUtils`是Apache Commons Lang库中的一个核心工具类,它提供了大量的静态方法,用于处理字符串。这个库在Java开发中非常常见,因为它弥补了Java标准库中对字符串操作的不足。`StringUtils`类包含了多种实用...

    commons-lang3-3.1 StringUtils字符串jar包

    commons-lang3-3.1 StringUtils字符串jar包 org.apache.commons.lang3.StringUtils的jar包

    StringUtils 字符串常用工具

    在Java编程语言中,`StringUtils` 是一个非常实用的工具类,它提供了大量关于字符串操作的方法,极大地简化了对字符串的处理。这个类通常在处理字符串时提高代码的可读性和效率,尤其在处理字符串的空值、拼接、分割...

    StringUtils函数全集

    ### StringUtils函数全集详解 #### 一、简介 在Java编程语言中,处理字符串是非常常见的需求之一。为了方便开发者高效地进行字符串操作,Apache Commons Lang库提供了一个强大的工具类——`StringUtils`。此工具类...

    StringUtils的用法

    \n\t\f\r") = "" //所有控制字符都被移除 StringUtils.trim("bob") = "bob" StringUtils.trim(" bob ") = "bob" 6. public static String trimToNull(String str) 和 trim() 类似,但结果为 null 时返回 null,而...

    B4A - StringUtils.rar_android_b4a_basic_strings_utilities

    "StringUtils.rar_android_b4a_basic_strings_utilities"这个压缩包,显然专注于介绍B4A中处理字符串的实用工具。在本教程中,我们将深入探讨B4A中的字符串处理函数和技巧,这对于任何想要提高其B4A编程技能的开发者...

Global site tag (gtag.js) - Google Analytics