Update 5. 替换空格.md
This commit is contained in:
parent
efeb9b3762
commit
cb2e60af8c
@ -51,7 +51,54 @@ public String replaceSpace(StringBuffer str) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public String replaceSpace(String s) {
|
||||||
|
return replaceSB2(s);
|
||||||
|
}
|
||||||
|
public String replaceAll(String s){
|
||||||
|
return s.replaceAll(" ", "%20");
|
||||||
|
}
|
||||||
|
public String replaceSB2(String s){
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for( int i = 0; i < s.length(); i++){
|
||||||
|
if(s.charAt(i) == ' '){
|
||||||
|
sb.append("%20");
|
||||||
|
}else{
|
||||||
|
sb.append(s.charAt(i));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
public String replaceSB(String s){
|
||||||
|
StringBuilder sb = new StringBuilder(s);
|
||||||
|
|
||||||
|
for( int i = 0; i < s.length(); i++){
|
||||||
|
if(s.charAt(i) == ' '){
|
||||||
|
sb.append("==");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = s.length()-1;
|
||||||
|
int j = sb.length()-1;
|
||||||
|
while(i<j && i >=0){
|
||||||
|
if(sb.charAt(i) == ' '){
|
||||||
|
sb.setCharAt(j--,'0');
|
||||||
|
sb.setCharAt(j--,'2');
|
||||||
|
sb.setCharAt(j--,'%');
|
||||||
|
}else{
|
||||||
|
sb.setCharAt(j--,sb.charAt(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user