Skip to content

Instantly share code, notes, and snippets.

@hilda8519
Created June 18, 2014 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilda8519/e4f3e5ade2e38224129a to your computer and use it in GitHub Desktop.
Save hilda8519/e4f3e5ade2e38224129a to your computer and use it in GitHub Desktop.
package replaceSpace;
public class replaceSpace {
public static void main(String[] args){
String s="My dog is maotou";
String t=add(s);
System.out.println(s);
System.out.println(t);
}
public static String add(String s){
int spacecount=0;
for (int i = 0; i < s.length(); i++){
if(s.charAt(i)==' ')
spacecount++;
}
char[] replace=new char[s.length()+spacecount*2];
int j=0;
for(int i=0;i<s.length();i++){
if (s.charAt(i)==' '){
replace[j]='%';
replace[j+1]='2';
replace[j+2]='0';
j+=2;
}
else
replace[j]=s.charAt(i);
j++;
}
String replaces=new String(replace);
return replaces;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment