关于字符串格式的替换

BobbyLou 发布于 2016/10/28 10:32
阅读 270
收藏 0

问题:某字符串中 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;
	}


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


加载中
1
螃蟹大叔
螃蟹大叔
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
太黑_thj
太黑_thj

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

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



BobbyLou
BobbyLou
我去..你赢了.啊啊啊啊
1
pantrick
pantrick
楼主思维僵化了
BobbyLou
BobbyLou
回复@抓瓦工人 : 已然僵化了..
0
BobbyLou
BobbyLou

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

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;
 }
}



略屌啊.跟你比,我简直弱爆了!
螃蟹大叔
螃蟹大叔
过奖了,哈哈
0
吴笑笑
字符串处理不用正则容易出错
BobbyLou
BobbyLou
正则学不明白..
0
kipeng300
kipeng300

你为什么不直接替换,呢,在字串后面再拼接^即可

有时候一种方法不行,你可以试着换一种思路


BobbyLou
BobbyLou
回复@kipeng300 : 因为我钻进去了.没出来.
返回顶部
顶部