单独的数字和字母

时间:2012-11-25 14:34:56

标签: java split letters-and-numbers

假设我有一个表示"Hello123"的字符串,如何将它们分隔成s[0] = "Hello", s[1] = "123"?我希望使用s.split(),但我不知道在参数/参数中放什么。

1 个答案:

答案 0 :(得分:3)

您可以使用正则表达式:

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");

分割

psdfh123sdkfjhsdf349287

psdfh
123
sdkfjhsdf
349287