6
回答
关于字符串格式的替换
开发十年,就只剩下这套Java开发体系了   

问题:某字符串中 1,2,3,4 替换成 ^1^2^3^4^

注意:要考虑数字是一位数,还是两位数

我已写好了Java代码.可是还是有问题

当含有两位数的时候,结果是我想要的

1,2,3,4,5,6,7,8,9,10,11
结果是:^1^2^3^4^5^6^7^8^9^10^11^ 

当不含两位数的时候,结果少了一个"^"

1,2,3,4,5,6,7,8,9
结果是: ^1^2^3^4^5^6^7^8 (少了一个^)
预期是: ^1^2^3^4^5^6^7^8^


public static void main(String[] args) {
		// TODO Auto-generated method stub
		String a = "1,2,3,4,5,6,7,8,9,10,11";
		String b = "1,2,3,4,5,6,7,8,9";
		
		System.out.println(replaceNum(a));
		System.out.println(replaceNum(b));

	}
	public static String replaceNum(String a) {
		String ssString = "";
		 for (int i = 0; i+1 < a.length(); i++) {
			 //数字0-9的ascii码值48-57
			 if(i+1==a.length()-1){
				 if (a.charAt(i)>=48&&a.charAt(i)<58) {
					  if (a.charAt(i+1)>=48&&a.charAt(i+1)<58) {
						  ssString +="^"+a.charAt(i)+a.charAt(i+1)+"^";
					  }else{
						  ssString +="^"+a.charAt(i)+"^"; 
					  }
				  }
			  }else{
				  if (a.charAt(i)>=48&&a.charAt(i)<58) {
					  if (a.charAt(i+1)>=48&&a.charAt(i+1)<58) {
						  ssString +="^"+a.charAt(i)+a.charAt(i+1); 
						  i++;
					  }else{
						  ssString +="^"+a.charAt(i); 
					  }
				  }
			  }
		}
		 return ssString;
	}


昨天研究自己写的代码没研究出好的办法,如果大家有更好的建议.还望指出不足.谢谢大家!


举报
BobbyLou
发帖于2年前 6回/267阅
共有6个答案 最后回答: 2年前
package com.yunzhijia.app.utils;


import org.apache.commons.lang.StringUtils;


public class Demo {
 public static void main(String[] args) {
 System.err.println(replaceNum("1,2,3,4,5,6,7,8,9,10,11"));
 System.err.println(replaceNum("1,2,3,4,5,6,7,8,9"));
 System.err.println(replaceNum("1a2,3#4,5as6,7,8,9"));
 System.err.println(replaceNum(",1,2,3,4,5,6,7,8,9"));
 System.err.println(replaceNum(",1,2,3,4,5,6,7,8,9,"));
 System.err.println(replaceNum("1,2,3,4,5,6,7,8,9,"));
 System.err.println(replaceNum2("1,2,3,4,5,6,7,8,9,10,11"));
 System.err.println(replaceNum2("1,2,3,4,5,6,7,8,9"));
 System.err.println(replaceNum2("1a2,3#4,5as6,7,8,9"));
 System.err.println(replaceNum2(",1,2,3,4,5,6,7,8,9"));
 System.err.println(replaceNum2(",1,2,3,4,5,6,7,8,9,"));
 System.err.println(replaceNum2("1,2,3,4,5,6,7,8,9,"));
 System.err.println(replaceNum("1#2#3"));
 System.err.println(replaceNum2("1#2#3"));
 }


 public static String replaceNum(String text) {
 String[] list = text.split("[^\\d]+");
 String result = StringUtils.join(list, "^");
 if (!result.startsWith("^")) {
 result = "^" + result;
 }
 if (!result.endsWith("^")) {
 result += "^";
 }
 return result;
 }
 public static String replaceNum2(String text) {
 String result = text.replaceAll("[^\\d]+", "^");
 if (!result.startsWith("^")) {
 result = "^" + result;
 }
 if (!result.endsWith("^")) {
 result += "^";
 }
 return result;
 }
}



哪里需要那么复杂啊,直接一个替换就搞定了,然后首尾加上符号

String s="";
s="^"+s.replaceAll(",", "^")+"^";



--- 共有 1 条评论 ---
BobbyLou我去..你赢了.啊啊啊啊 2年前 回复

引用来自“刘万杰”的评论

package com.yunzhijia.app.utils;


import org.apache.commons.lang.StringUtils;


public class Demo {
 public static void main(String[] args) {
 System.err.println(replaceNum("1,2,3,4,5,6,7,8,9,10,11"));
 System.err.println(replaceNum("1,2,3,4,5,6,7,8,9"));
 System.err.println(replaceNum("1a2,3#4,5as6,7,8,9"));
 System.err.println(replaceNum(",1,2,3,4,5,6,7,8,9"));
 System.err.println(replaceNum(",1,2,3,4,5,6,7,8,9,"));
 System.err.println(replaceNum("1,2,3,4,5,6,7,8,9,"));
 System.err.println(replaceNum2("1,2,3,4,5,6,7,8,9,10,11"));
 System.err.println(replaceNum2("1,2,3,4,5,6,7,8,9"));
 System.err.println(replaceNum2("1a2,3#4,5as6,7,8,9"));
 System.err.println(replaceNum2(",1,2,3,4,5,6,7,8,9"));
 System.err.println(replaceNum2(",1,2,3,4,5,6,7,8,9,"));
 System.err.println(replaceNum2("1,2,3,4,5,6,7,8,9,"));
 System.err.println(replaceNum("1#2#3"));
 System.err.println(replaceNum2("1#2#3"));
 }


 public static String replaceNum(String text) {
 String[] list = text.split("[^\\d]+");
 String result = StringUtils.join(list, "^");
 if (!result.startsWith("^")) {
 result = "^" + result;
 }
 if (!result.endsWith("^")) {
 result += "^";
 }
 return result;
 }
 public static String replaceNum2(String text) {
 String result = text.replaceAll("[^\\d]+", "^");
 if (!result.startsWith("^")) {
 result = "^" + result;
 }
 if (!result.endsWith("^")) {
 result += "^";
 }
 return result;
 }
}



略屌啊.跟你比,我简直弱爆了!
--- 共有 1 条评论 ---
螃蟹大叔过奖了,哈哈 2年前 回复
顶部