简体   繁体   中英

Split a string into array without empty element

Attempting to split a string into separated letters

String string1 ="KKXGJRNQGA";
List<String> solutionArray = Arrays.asList(string1.split(""));

will return [, K, K, X, G, J, R, N, Q, G, A] before each alphabet a space and at [0] is an empty element.
But actually i want is [K,K,X,G,J,R,N,Q,G,A] , is there a way to solve it? can using regex match?

Using string1.split("") will return an empty first value.

Use toCharArray() which converts the string to a new character array.

"KKXGJRNQGA".toCharArray()

or a simple regular expression

String s = "KKXGJRNQGA";
String[] p = s.split("(?!^)");
System.out.println(Arrays.asList(p));

Output:

[K, K, X, G, J, R, N, Q, G, A]

怎么样

char[] letters = string1.toCharArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM