2016-12-04 58 views
0

我需要能够将字符串拆分为字母和数字。它会看到的字符串是沿线在字符串中分割字符和数字在

"dir1" "path11" 

我还需要暂时存储2个值。我找到了.split方法,它看起来可能是我之后的,但我无法弄清楚如何使用它,我应该继续研究它还是有更好的方法?

+0

是弦总是**字母后跟数字* *? –

+0

在这种情况下,他们将是 –

+0

这是类似的解决方案在SO http://stackoverflow.com/questions/8270784/how-to-split-a-string-between-letters-and-digits-or-between-digits-和 - 字母或http://stackoverflow.com/questions/16787099/how-to-split-the-string-into-string-and-integer-in-java – dkb

回答

0

正则表达式和拆分可以帮助做到这一点:

public static void main(String[] args) { 
String[] c = "path11".split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"); 
System.out.println(Arrays.toString(c)); 
} 

输出=

[路,11]

0

使用String.split()作为在

String[] strings = "abc111".split("(?=\\d+)(?<=\\D+)"); 

split()函数采用围绕该字符串将被分割了一个正则表达式。

这里模式有两个断言:一个断言前面的字符只是数字,另一个断言前面的字符是非数字。所以它将在非数字结束之后(即在数字开始之前)分开。