java分割字符串中的汉字和数字问题

2025-02-24 01:01:08
推荐回答(2个)
回答1:

可以用正则:

import java.util.regex.*;

public class test {

public static void main( String[] args ) {
String s = "字符串123456哈哈441";
Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]+|\\d+");
Matcher m = p.matcher( s );
while ( m.find() ) {
System.out.println( m.group() );
}
}
}

运行结果:
字符串
123456
哈哈
441

回答2:

楼上的回答很正确, 也可以用Ascii码,,..如果把s中的123456赋予一个变量s1:try{
int s1 = Integer.parse(s);
}catch( NumberFormatException ex ){
}