0

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?

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

1 Comment

I don't understood how this regex works "(?!^)" what is it mean? how is it works ? please explain thanks
1

How about

char[] letters = string1.toCharArray();

1 Comment

... and you can change that to a List or to Strings later (or ideally use as is).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.