String array and string conversion

1.StringUtils.join(Object array[],String separator)  

Forms an array into a new string separated by symbols or other strings

 

Object array[] The array to be converted. separator The interval symbol that forms a new string, such as "," "|"

private static final String[] str = {"1","2","3","4"};
2 String str2 = StringUtils.join(str, "|");
3
4 System.out.println(str2);

 Output result: 1|2|3|4

Reprinted from: https://www.cnblogs.com/astrocc/archive/2012/10/20/2732733.html

 2. spit() method in java

Basic format: stringObj.split([separator, [limit]]) parameters, in which each part, stringObj represents the string to be split, separator represents the string or regular expression object, which identifies the separated string whether one or more characters are used. If this option is omitted, a single-element array containing the entire string is returned; limit : indicates that this value is used to limit the number of elements in the returned array.

This method returns an array of strings, if special characters are encountered, such as regular expressions, for example: | , + , * , ^ , $ , / , | , [ , ] , ( , ) , - , . , \ etc, need \ to escape

For example: if you want to use | vertical bar to separate a character, because | itself is part of the regular expression, you need \ to escape, because the escape uses \, and this \ happens to be a regular expression character, so you have to Use a \ , so two \\ are needed.                If you use "." as a separator, it must be written as follows: String.split("\\."), if you use "|" as a separator, it must be written as follows: String.split("\\|"),       If there are multiple separators in a string, you can use "|" as a hyphen, for example: "a=1 and b =2 or c=3", to separate all three, you can use String.split( "and|or");

 

2. Convert an array of strings to strings

example:

String[] str = {"abc", "bcd", "def"};
StringBuffer sb = new StringBuffer();
for(int i = 0; i < str.length; i++){
 sb. append(str[i]);
}
String s = sb.toString();

 

 If it is "character array" turn to "string"

char[]   data={'a','b','c'};   
String  s=new   String(data);

 Reprinted from: https://www.cnblogs.com/SharkBin/p/4512561.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324838096&siteId=291194637