Skip to content

Commit 8f41ea5

Browse files
测试ReplaceFirst对空格的替换效果
1 parent 487c950 commit 8f41ea5

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.string;
2+
3+
/**
4+
* @Author: EnjoyCoding
5+
* @Date: 2020\4\9 0009 23:16
6+
* @Description:
7+
*/
8+
public class ReplaceFirstDemo {
9+
10+
public static void main(String[] args) {
11+
String test="a b c defg ";
12+
int count = replaceSpaceCount(test);
13+
System.out.println(count);
14+
}
15+
16+
public static int replaceSpaceCount(String str) {
17+
if (str == null) {
18+
return 0;
19+
}
20+
int count=0;
21+
while (str.contains(" ")) {
22+
str = str.replaceFirst(" ", "");
23+
count++;
24+
}
25+
System.out.println(str);
26+
return count;
27+
}
28+
29+
30+
}

0 commit comments

Comments
 (0)