0

I know there lots of way splitting a string to array. but i wonder is there a regex that i can use it to split all string chars to a String[] array ?

For example : "foo"->String[] { "f","o","o" }

String input="foo";
String[] split=input.split( .... )
1

2 Answers 2

4

Try this :

String []split = input.split("(?!^)");

but : String []split = input.split(""); will get you {"","f","o","o"}

Sign up to request clarification or add additional context in comments.

2 Comments

Actually this explanation is wrong. (?!..) is look-ahead, not look-behind, so it will split on every place which has no start of the string (^) after it (but it still works because both, ^ and checked places are zero-width).
@Pshemo: you alright it's mean "zero-width negative lookahead"
1

You can try this:-

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String input="foo";
        String[] split=input.split("");
        for(int x=0;x<split.length;x++){
            System.out.println("Data index::"+split[x]);
        }
    }

Output:-

Data index::f
Data index::o
Data index::o

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.