0

I am using Java and want to split a string without deleting any of the content. Here are 2 examples, I wish to split String hi = "HELLO123" into "HELLO" and "123" and I wish to split "TEST123" into "TEST" and "123". Is there any java code that can do such a task? (It will always split the String between an Integer and an uppercase letter from the English language) I tried using .substring() and .split()

1 Answers1

0

This should do the trick:

public class HelloWorld{

     public static void main(String []args){
        String s = "Hello0world";
        String[] split = s.split("(?<=[0-9]+)");
        System.out.println(split[0]);
        System.out.println(split[1]);
     }
}

(?<=pattern) Is a positive lookbehind. It means that it'll match right after the pattern, in this case [0-9]+ (one or more numbers).

Federico Nafria
  • 1,397
  • 14
  • 39