Separate Numbers and Letters

瘦欲@ 提交于 2019-12-11 11:25:55

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/13551985/separate-numbers-and-letters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!