简体   繁体   English

单独的数字和字母

[英]Separate Numbers and Letters

Let's say I have a String that says "Hello123" , how can I separate them to become s[0] = "Hello", s[1] = "123" ? 假设我有一个表示"Hello123"的字符串,如何将它们分隔成s[0] = "Hello", s[1] = "123" I wish to use s.split() but I don't know what to put in the argument/parameter. 我希望使用s.split()但我不知道在参数/参数中放什么。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM