java 用逗号分割字符串 Java:为什么使用split分隔字符串时,分隔"?"字符需...
\u7528java\u5c06\u201done, two, there, four\u201d\u4ee5\u9017\u53f7\u5206\u5272\u7684\u5b57\u7b26\u4e32\u5206\u89e3String s = "one, two, there, four";
String[] as = s.split(", ");
for (int i = 0; i < as.length; i++) {
System.out.println(as[i]);
}
\u56e0\u4e3a\u67e5\u770bapi\uff0c\u4f60\u5c31\u4f1a\u53d1\u73b0String.split(String regex)\uff0c\u4e5f\u5c31\u662f\u8bf4\u91cc\u9762\u7684\u53c2\u6570\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u3002
\u5982\u679c\u662f\u4e00\u4e9b\u666e\u901a\u7684\u5b57\u7b26\uff0c\u5b83\u5c31\u4f1a\u5f53\u505a\u666e\u901a\u5b57\u7b26\u7ed9\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u53ef\u662f ?\u662f\u7279\u6b8a\u5b57\u7b26\uff0c\u60f3\u8ba9\u6309\u7167 \u5b83\u6765\u5206\uff0c\u6240\u4ee5\u5c31\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8868\u793a\u67d0\u4e2a\u7279\u5b9a\u5b57\u7b26\u7684\u65b9\u5f0f\u4e86\u3002
\u4e5f\u5c31\u662f\u8bf4[?] \u5c31\u8868\u793a \u95ee\u53f7
1\u3001Java\u662f\u4e00\u79cd\u53ef\u4ee5\u64b0\u5199\u8de8\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\u7684\u9762\u5411\u5bf9\u8c61\u7684\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u3002Java \u6280\u672f\u5177\u6709\u5353\u8d8a\u7684\u901a\u7528\u6027\u3001\u9ad8\u6548\u6027\u3001\u5e73\u53f0\u79fb\u690d\u6027\u548c\u5b89\u5168\u6027\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8ePC\u3001\u6570\u636e\u4e2d\u5fc3\u3001\u6e38\u620f\u63a7\u5236\u53f0\u3001\u79d1\u5b66\u8d85\u7ea7\u8ba1\u7b97\u673a\u3001\u79fb\u52a8\u7535\u8bdd\u548c\u4e92\u8054\u7f51\uff0c\u540c\u65f6\u62e5\u6709\u5168\u7403\u6700\u5927\u7684\u5f00\u53d1\u8005\u4e13\u4e1a\u793e\u7fa4\u3002
2\u3001Java\u7684\u4e3b\u8981\u5de5\u4f5c\uff1a\u901a\u8fc7\u7f16\u7a0b\u8bed\u8a00\u6765\u5236\u4f5c\u4e92\u8054\u7f51\u9875\u9762\u3001\u5236\u4f5c\u52a8\u6001\u6548\u679c\u4ee5\u53ca\u7f51\u7ad9\u7b49\u6280\u672f\uff0c\u73b0\u5728\u7f51\u4e0a\u5173\u4e8e\u8fd9\u65b9\u9762\u7684\u6559\u7a0b\u6709\u5f88\u591a\uff0c\u4f46\u5f88\u591a\u90fd\u662f\u8fc7\u65f6\u7684\uff0c\u56e0\u4e3a\u4e92\u8054\u7f51\u662f\u5728\u4e0d\u65ad\u7684\u53d8\u5316\u7684\uff0c\u4e92\u8054\u7f51\u4e0a\u7684\u725b\u4eba\uff0c\u8981\u60f3\u6210\u4e3aJava\u5927\u795e\uff0c\u8d70\u8fdb\u8fd9\u6761Java\u5927\u795e\u4e4b\u7fa4\uff0c\u9996\u5148Java\u6559\u7a0b\u7684\u5f00\u5934\u662f\u4e8c\u4e09\u4e00\uff0cJava\u7684\u6700\u4e2d\u95f4\u662f\u4e09\u4e00\u56db\uff0cJava\u7684\u6700\u540e\u9762\u518d\u52a0\u4e0a\u96f6\u4e8c\u516b\uff0c\u628a\u524d\u9762\u7684\u7efc\u5408\u8d77\u6765\uff0cJava\u5927\u795e\u671f\u5f85\u4f60\u7684\u52a0\u5165\u3002
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String str = "2005a-3.pt.5";
Pattern p = Pattern.compile("[(\\d[^\\d])[([^\\d]\\d)][\\d][^\\d]]+");
Matcher m = p.matcher(str);
String resultStr = "";
while(m.find()) {
if(m.group().matches("\\d+\\b|[^\\d]+\\b")) {
resultStr = resultStr +m.group()+",";
}
else {
p = Pattern.compile("\\d+|[^\\d]+");
Matcher mm = p.matcher(m.group());
while(mm.find()) {
resultStr = resultStr+mm.group()+",";
}
}
}
System.out.println(resultStr.substring(0, resultStr.length()-1));
}
}
上面是用正则的,其实你可以不用正则,而是去遍历这个字符串,然后比较他们的ASCII码是不是数字。
你可以用替换法将数字和非数字用逗号隔开,Java程序如下:
public class Z {
public static void main(String[] args) {
String str="2005a-3.pt.5";
String a=str.replaceAll("(\\D+)",",$1,");
System.out.println(a);
}
}
运行结果:
2005,a-,3,.pt.,5
String str="2005a-3.pt.5";
String a=str.replaceAll("(\\D+)",",$1,");
System.out.println(a);
正则匹配,或者使用ASCII码
用 charAt 去取 然后 判断取出来的字符是否在数字所在的整形对应区间
最后就是拼接数组.如果你觉得需要这个算法 就追问,上面兄弟的那些也能用
扩展阅读:java println ... java substring截取字符串 ... java字符串分割split ... java split分割数组 ... jquery ... java 字符串包含 ... java截取字符串后两位 ... java string字符串分割 ... java截取字符串的一部分 ...