java service - common technology - uncommon functions, methods, techniques

                

1. String operations

1. Characters that need to be escaped

java字符串中需要转义的特殊字符

1. \n 表示换行;

2. \t 表示制表符,相当于Table键;

3. \' 表示单引号;

4. \" 表示双引号;

5. \\ 表示一个斜杠“\”。

2. Usage of the second parameter limit of split

public String[] split(String regex , int limit)

Splits this string based on matching the given regular expression. 
The array returned by this method contains substrings of this string, each terminated by another substring matching the given expression, or by the end of this string. The substrings in the array are sorted in the order they appear in this string. If the expression does not match any part of the input, the resulting array has exactly one element, this string. 

The limit parameter controls how many times the pattern is applied, thus affecting the length of the resulting array.

If n > 0, the pattern will be applied at most n - 1 times, the length of the array will be no greater than n, and the last item of the array will contain all input beyond the last matched delimiter.

If n < 0, the pattern will be applied as many times as possible, and the array can be of any length.

If n = 0, then the pattern will be applied as many times as possible, the array can be of any length, and trailing empty strings will be discarded.

 For example, the string "boo:and:foo" produces the following results using these parameters: 

Regex Limit result 
first parameter ":" second parameter "2" result → { "boo", "and:foo" } 
first parameter ":" second parameter "5" result → { "boo" , "and", "foo" } 
first argument ":" second argument "-2" result → { "boo", "and", "foo" } first 
argument "o" second argument "5" result → { "b", "", ":and:f", "", "" } first 
parameter "o" second parameter "-2" result → { "b", "" , ":and:f", "", "" } 
first parameter "o" second parameter "0" result → { "b", "", ":and:f" }

3. Timestamp

Get the int type timestamp, accurate to seconds, Java8 provides the following method

 (int) Instant.now().getEpochSecond()

4. String to Set

public static HashSet<String> toSet(String txt) {
        HashSet<String> set = new HashSet<>();
        if (!StringUtils.isBlank(txt)) {
            String[] items = txt.split(",");
            set = Arrays.stream(items).map(String::trim).collect(Collectors.toCollection(HashSet::new));
        }
        return set;
    }

5. Remove the leading and trailing spaces from the string

The trim() method in Java String is a built-in function that removes leading and trailing spaces. The Unicode value for the space character is '\u0020'. The trim() method in java checks this Unicode value before and after the string, removes spaces if present and returns the omitted string.

public String trim()

Parameters: The trim() method does not accept any parameters.
Return type: The return type of the trim() method is String. It returns the omitted string without leading and trailing spaces

 

Guess you like

Origin blog.csdn.net/philip502/article/details/130719865