StringUtils library detailed explanation and code examples

StringUtils is a utility class in the Apache Commons Lang library that provides many practical methods to process strings. It contains various string operation functions, making string processing more convenient and efficient.

The following are some common methods of the StringUtils library and explanations of their functions:

  1. isEmpty(CharSequence str): Checks whether the given string is empty or has length 0. It accepts a CharSequence object (such as String) as parameter and returns a Boolean value indicating whether the string is empty.
String str = "Hello";
boolean isEmpty = StringUtils.isEmpty(str);
// isEmpty的值为false
  1. isNotEmpty(CharSequence str): Checks whether the given string is not empty and has a length greater than 0. It accepts a CharSequence object as a parameter and returns a Boolean value indicating whether the string is not empty.
String str = "Hello";
boolean isNotEmpty = StringUtils.isNotEmpty(str);
// isNotEmpty的值为true
  1. isBlank(CharSequence str): Checks whether the given string is empty, has length 0 or contains only spaces. It accepts a CharSequence object as a parameter and returns a Boolean value indicating whether the string is blank.
String str = "   ";
boolean isBlank = StringUtils.isBlank(str);
// isBlank的值为true
  1. isNotBlank(CharSequence str): Checks whether the given string is not empty, has a length greater than 0 and contains not only spaces. It accepts a CharSequence object as a parameter and returns a Boolean value indicating whether the string is not whitespace.
String str = "   ";
boolean isNotBlank = StringUtils.isNotBlank(str);
// isNotBlank的值为false
  1. trimToNull(String str): Removes leading and trailing spaces from the given string and returns the processed result. If the processed string is empty or has a length of 0, null is returned.
String str = "  Hello  ";
String trimmedStr = StringUtils.trimToNull(str);
// trimmedStr的值为"Hello"
  1. trimToEmpty(String str): Removes leading and trailing spaces from the given string and returns the processed result. If the processed string is empty or has a length of 0, an empty string is returned.
String str = "  Hello  ";
String trimmedStr = StringUtils.trimToEmpty(str);
// trimmedStr的值为"Hello"
  1. substring(String str, int start): Returns the substring of the given string starting from the specified index to the end. It accepts a string and a starting index as parameters and returns the substring starting from the starting index to the end of the string.
String str = "Hello, World!";
String substring = StringUtils.substring(str, 7);
// substring的值为"World!"
  1. substring(String str, int start, int end): Returns the substring of the given string from the specified start index to the specified end index. It accepts a string, starting index, and ending index as parameters and returns the substring within the specified range.
String str = "Hello, World!";
String substring = StringUtils.substring(str, 7, 12);
// substring的值为"World"
  1. capitalize(String str): Converts the first character of the given string to uppercase and returns the processed result. If the string is empty or has length 0, the original string is returned.
String str = "hello";
String capitalizedStr = StringUtils.capitalize(str);
// capitalizedStr的值为"Hello"
  1. uncapitalize(String str): Converts the first character of the given string to lowercase and returns the processed result. If the string is empty or has length 0, the original string is returned.
String str = "Hello";
String uncapitalizedStr = StringUtils.uncapitalize(str);
// uncapitalizedStr的值为"hello"
  1. equals(CharSequence str1, CharSequence str2): Compares two strings for equality, ignoring case. It accepts two CharSequence objects as parameters and returns a Boolean value indicating whether the two strings are equal.
String str1 = "Hello";
String str2 = "hello";
boolean equalsIgnoreCase = StringUtils.equalsIgnoreCase(str1, str2);
// equalsIgnoreCase的值为true

These are just some of the common methods provided by StringUtils, there are many other useful string manipulation methods available. By using StringUtils, developers can more easily handle various needs of strings, improving the readability and maintainability of code.

Here is a complete example code using the StringUtils library:

import org.apache.commons.lang3.StringUtils;

public class StringUtilsExample {
    
    
    public static void main(String[] args) {
    
    
        String str = "  Hello, World!  ";
        
        // 检查字符串是否为空或长度为0
        boolean isEmpty = StringUtils.isEmpty(str);
        System.out.println("Is empty: " + isEmpty);
        
        // 检查字符串是否不为空且长度大于0
        boolean isNotEmpty = StringUtils.isNotEmpty(str);
        System.out.println("Is not empty: " + isNotEmpty);
        
        // 检查字符串是否为空、长度为0或仅包含空格
        boolean isBlank = StringUtils.isBlank(str);
        System.out.println("Is blank: " + isBlank);
        
        // 检查字符串是否不为空、长度大于0且不仅包含空格
        boolean isNotBlank = StringUtils.isNotBlank(str);
        System.out.println("Is not blank: " + isNotBlank);
        
        // 删除字符串开头和结尾的空格并返回处理后的结果
        String trimmedStr = StringUtils.trimToNull(str);
        System.out.println("Trimmed string: " + trimmedStr);
        
        // 截取字符串从指定索引开始到结尾的子字符串
        String substring = StringUtils.substring(str, 2);
        System.out.println("Substring: " + substring);
        
        // 将字符串的第一个字符转换为大写
        String capitalizedStr = StringUtils.capitalize(str);
        System.out.println("Capitalized string: " + capitalizedStr);
        
        // 比较两个字符串是否相等,忽略大小写
        boolean equalsIgnoreCase = StringUtils.equalsIgnoreCase("Hello", "hello");
        System.out.println("Equals (ignore case): " + equalsIgnoreCase);
    }
}

In the above example, we have used various methods of StringUtils to demonstrate its functionality. You can use different StringUtils methods to process strings according to specific needs, simplifying the code and improving efficiency. Remember to make sure you have added the Apache Commons Lang library to your project dependencies before using StringUtils. StringUtils is a utility class in the Apache Commons Lang library that provides many practical methods to process strings. It contains various string operation functions, making string processing more convenient and efficient.

The following are some common methods of the StringUtils library and explanations of their functions:

  1. isEmpty(CharSequence str): Checks whether the given string is empty or has length 0. It accepts a CharSequence object (such as String) as parameter and returns a Boolean value indicating whether the string is empty.
String str = "Hello";
boolean isEmpty = StringUtils.isEmpty(str);
// isEmpty的值为false
  1. isNotEmpty(CharSequence str): Checks whether the given string is not empty and has a length greater than 0. It accepts a CharSequence object as a parameter and returns a Boolean value indicating whether the string is not empty.
String str = "Hello";
boolean isNotEmpty = StringUtils.isNotEmpty(str);
// isNotEmpty的值为true
  1. isBlank(CharSequence str): Checks whether the given string is empty, has length 0 or contains only spaces. It accepts a CharSequence object as a parameter and returns a Boolean value indicating whether the string is blank.
String str = "   ";
boolean isBlank = StringUtils.isBlank(str);
// isBlank的值为true
  1. isNotBlank(CharSequence str): Checks whether the given string is not empty, has a length greater than 0 and contains not only spaces. It accepts a CharSequence object as a parameter and returns a Boolean value indicating whether the string is not whitespace.
String str = "   ";
boolean isNotBlank = StringUtils.isNotBlank(str);
// isNotBlank的值为false
  1. trimToNull(String str): Removes leading and trailing spaces from the given string and returns the processed result. If the processed string is empty or has a length of 0, null is returned.
String str = "  Hello  ";
String trimmedStr = StringUtils.trimToNull(str);
// trimmedStr的值为"Hello"
  1. trimToEmpty(String str): Removes leading and trailing spaces from the given string and returns the processed result. If the processed string is empty or has a length of 0, an empty string is returned.
String str = "  Hello  ";
String trimmedStr = StringUtils.trimToEmpty(str);
// trimmedStr的值为"Hello"
  1. substring(String str, int start): Returns the substring of the given string starting from the specified index to the end. It accepts a string and a starting index as parameters and returns the substring starting from the starting index to the end of the string.
String str = "Hello, World!";
String substring = StringUtils.substring(str, 7);
// substring的值为"World!"
  1. substring(String str, int start, int end): Returns the substring of the given string from the specified start index to the specified end index. It accepts a string, starting index, and ending index as parameters and returns the substring within the specified range.
String str = "Hello, World!";
String substring = StringUtils.substring(str, 7, 12);
// substring的值为"World"
  1. capitalize(String str): Converts the first character of the given string to uppercase and returns the processed result. If the string is empty or has length 0, the original string is returned.
String str = "hello";
String capitalizedStr = StringUtils.capitalize(str);
// capitalizedStr的值为"Hello"
  1. uncapitalize(String str): Converts the first character of the given string to lowercase and returns the processed result. If the string is empty or has length 0, the original string is returned.
String str = "Hello";
String uncapitalizedStr = StringUtils.uncapitalize(str);
// uncapitalizedStr的值为"hello"
  1. equals(CharSequence str1, CharSequence str2): Compares two strings for equality, ignoring case. It accepts two CharSequence objects as parameters and returns a Boolean value indicating whether the two strings are equal.
String str1 = "Hello";
String str2 = "hello";
boolean equalsIgnoreCase = StringUtils.equalsIgnoreCase(str1, str2);
// equalsIgnoreCase的值为true

These are just some of the common methods provided by StringUtils, there are many other useful string manipulation methods available. By using StringUtils, developers can more easily handle various needs of strings, improving the readability and maintainability of code.

Here is a complete example code using the StringUtils library:

import org.apache.commons.lang3.StringUtils;

public class StringUtilsExample {
    
    
    public static void main(String[] args) {
    
    
        String str = "  Hello, World!  ";
        
        // 检查字符串是否为空或长度为0
        boolean isEmpty = StringUtils.isEmpty(str);
        System.out.println("Is empty: " + isEmpty);
        
        // 检查字符串是否不为空且长度大于0
        boolean isNotEmpty = StringUtils.isNotEmpty(str);
        System.out.println("Is not empty: " + isNotEmpty);
        
        // 检查字符串是否为空、长度为0或仅包含空格
        boolean isBlank = StringUtils.isBlank(str);
        System.out.println("Is blank: " + isBlank);
        
        // 检查字符串是否不为空、长度大于0且不仅包含空格
        boolean isNotBlank = StringUtils.isNotBlank(str);
        System.out.println("Is not blank: " + isNotBlank);
        
        // 删除字符串开头和结尾的空格并返回处理后的结果
        String trimmedStr = StringUtils.trimToNull(str);
        System.out.println("Trimmed string: " + trimmedStr);
        
        // 截取字符串从指定索引开始到结尾的子字符串
        String substring = StringUtils.substring(str, 2);
        System.out.println("Substring: " + substring);
        
        // 将字符串的第一个字符转换为大写
        String capitalizedStr = StringUtils.capitalize(str);
        System.out.println("Capitalized string: " + capitalizedStr);
        
        // 比较两个字符串是否相等,忽略大小写
        boolean equalsIgnoreCase = StringUtils.equalsIgnoreCase("Hello", "hello");
        System.out.println("Equals (ignore case): " + equalsIgnoreCase);
    }
}
  1. countMatches(CharSequence str, CharSequence sub): Counts the number of occurrences of a specified substring in a string. It takes a source string and a substring to match, and returns the number of times the substring occurs in the source string.
String str = "Hello, Hello, Hello!";
int count = StringUtils.countMatches(str, "Hello");
// count的值为3
  1. indexOfAny(CharSequence str, CharSequence… searchStrs): Find the first matching index of any given substring in the source string. It accepts a source string and one or more substrings to search for, and returns the index of the first matching substring.
String str = "Hello, World!";
int index = StringUtils.indexOfAny(str, "o", "W");
// index的值为4
  1. leftPad(String str, int size, char padChar): Pads the specified characters on the left side of the given string until the string reaches the specified length. If the length of the given string is already equal to or exceeds the specified length, the original string is returned.
String str = "Hello";
String paddedStr = StringUtils.leftPad(str, 10, '*');
// paddedStr的值为 "***Hello"
  1. rightPad(String str, int size, char padChar): Pads the specified characters on the right side of the given string until the string reaches the specified length. If the length of the given string is already equal to or exceeds the specified length, the original string is returned.
String str = "Hello";
String paddedStr = StringUtils.rightPad(str, 10, '*');
// paddedStr的值为 "Hello*****"
  1. swapCase(String str): Converts uppercase letters in the string to lowercase letters, and lowercase letters to uppercase letters, and returns the processed result.
String str = "Hello, World!";
String swappedStr = StringUtils.swapCase(str);
// swappedStr的值为 "hELLO, wORLD!"
  1. substringBefore(String str, String separator): Returns the part of the given string before the specified separator. It accepts a source string and a delimiter as parameters and returns the substring before the delimiter.
String str = "Hello, World!";
String substring = StringUtils.substringBefore(str, ",");
// substring的值为 "Hello"
  1. substringAfter(String str, String separator): Returns the part of the given string after the specified separator. It accepts a source string and a delimiter as parameters and returns the substring after the delimiter.
String str = "Hello, World!";
String substring = StringUtils.substringAfter(str, ",");
// substring的值为 " World!"

These methods provide more different string processing functions, and you can choose the appropriate method to operate strings according to specific needs. The StringUtils library is very feature-rich and can greatly simplify string processing work and improve efficiency. Remember to make sure you have added the Apache Commons Lang library to your project dependencies before using StringUtils.

Guess you like

Origin blog.csdn.net/qq_46020806/article/details/132621713