String#split
method simply searches for places to split, it can't add anything new to result. In other words you can't split like
"abc"` -> ["ab", "bc"]
because b
can appear in only one element in resulting array.
What you can do is use Pattern
/Matcher
, specifically Matcher#find()
method and consume one character at a time, while "peeking" at next character without consuming it. The "peeking" part can be achieved with look-around mechanism because it is zero-length technique.
Demo:
Pattern p = Pattern.compile(".(?=(.))");
Matcher m = p.matcher(yourString);
while(m.find()){
System.out.println(m.group() + m.group(1));
}
Alternatively you can also use String#substring(start, end)
String data = "Testing";
for (int i=0; i<data.length()-1; i++){
System.out.println(data.substring(i,i+2));
}
or using String#charAt(index)
String data = "Testing";
for (int i = 0; i < data.length() - 1;i++) {
System.out.append(data.charAt(i)).println(data.charAt(i+1));
}