标签:
public class StringreplaceSpace { public static void main(String[] args){ StringBuffer str=new StringBuffer("We Are Happy"); System.out.println(replaceSpace(str)); } public static String replaceSpace(StringBuffer str) { int count=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)==‘ ‘){ count++; } } char str2[] = new char[str.length()+count*2]; for(int j=str.length()-1;j>=0;j--){ char temp = str.charAt(j); if(temp!=‘ ‘){ str2[j+2*count] =temp; } else{ str2[j+2*count]=‘0‘; str2[j+2*count-1]=‘2‘; str2[j+2*count-2]=‘%‘; count--; } } StringBuffer str3=new StringBuffer(); for(int k=0;k<str2.length;k++){ str3.append(str2[k]); } return str3.toString(); } }
标签:
原文地址:http://www.cnblogs.com/snowwhite/p/4747298.html