Regex for string splitting not working properly

Viewed 393

I want to split a string in 2 characters without any delimiter, but Regex split is not working properly

here is my code:-

  String str="splitstring";
    System.out.println("Split.."+str.trim().split("(?<=\\G.{2})").length);
    System.out.println("Split.."+str.trim().split("(?<=\\G.{2})")[0]);
    System.out.println("Split.."+str.trim().split("(?<=\\G.{2})")[1]);

output:-

 Split..2

 Split..sp

 Split..litstring
3 Answers
Related