1

Let's say I have a String that says "Hello123", how can I separate them to become s[0] = "Hello", s[1] = "123"? I wish to use s.split() but I don't know what to put in the argument/parameter.

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
e-zard Yusof
  • 25
  • 1
  • 4
  • first, find a substring that is one of the characters 1-9, then split at that location. – Randy Nov 25 '12 at 14:37

1 Answers1

4

You could use a regular expression:

String[] splitArray = subjectString.split(
    "(?x)                  # verbose regex mode on                    \n" +
    "(?<=                  # Assert that the previous character is... \n" +
    " \\p{L}               # a letter                                 \n" +
    ")                     # and                                      \n" +
    "(?=                   # that the next character is...            \n" +
    " \\p{N}               # a digit.                                 \n" +
    ")                     #                                          \n" +
    "|                     # Or                                       \n" +
    "(?<=\\p{N})(?=\\p{L}) # vice versa");

splits

psdfh123sdkfjhsdf349287

into

psdfh
123
sdkfjhsdf
349287
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561