Created
June 18, 2014 03:08
-
-
Save hilda8519/e4f3e5ade2e38224129a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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