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( .... )
blackpanther
  • 10,998
  • 11
  • 48
  • 78
Pooya
  • 862
  • 7
  • 10

2 Answers2

4

Try this :

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

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

Alya'a Gamal
  • 5,624
  • 19
  • 34
  • 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 Mar 17 '14 at 11:43
  • @Pshemo: you alright it's mean "zero-width negative lookahead" – Alya'a Gamal Mar 17 '14 at 11:52
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
JDGuide
  • 6,239
  • 12
  • 46
  • 64