在Java中寻找快速、简单的方法来更改此字符串
" hello there "
变成了这样的东西
"hello there"
在这里我用一个空格替换了所有这些多个空格,除了我还希望字符串开头的一个或多个空格消失。
像这样的事情能让我在一定程度上做到
String mytext = " hello there ";
mytext = mytext.replaceAll("( )+", " ");
但事实并非如此。
在Java中寻找快速、简单的方法来更改此字符串
" hello there "
变成了这样的东西
"hello there"
在这里我用一个空格替换了所有这些多个空格,除了我还希望字符串开头的一个或多个空格消失。
像这样的事情能让我在一定程度上做到
String mytext = " hello there ";
mytext = mytext.replaceAll("( )+", " ");
但事实并非如此。
当前回答
检查下这个吗……
public static void main(String[] args) {
String s = "A B C D E F G\tH I\rJ\nK\tL";
System.out.println("Current : "+s);
System.out.println("Single Space : "+singleSpace(s));
System.out.println("Space count : "+spaceCount(s));
System.out.format("Replace all = %s", s.replaceAll("\\s+", ""));
// Example where it uses the most.
String s = "My name is yashwanth . M";
String s2 = "My nameis yashwanth.M";
System.out.println("Normal : "+s.equals(s2));
System.out.println("Replace : "+s.replaceAll("\\s+", "").equals(s2.replaceAll("\\s+", "")));
}
如果字符串只包含单个空格,则replace()将不替换,
如果空格多于一个,则执行replace()操作并删除空格。
public static String singleSpace(String str){
return str.replaceAll(" +| +|\t|\r|\n","");
}
计算字符串中的空格数。
public static String spaceCount(String str){
int i = 0;
while(str.indexOf(" ") > -1){
//str = str.replaceFirst(" ", ""+(i++));
str = str.replaceFirst(Pattern.quote(" "), ""+(i++));
}
return str;
}
pattern .quote("?")返回字面模式字符串。
其他回答
检查下这个吗……
public static void main(String[] args) {
String s = "A B C D E F G\tH I\rJ\nK\tL";
System.out.println("Current : "+s);
System.out.println("Single Space : "+singleSpace(s));
System.out.println("Space count : "+spaceCount(s));
System.out.format("Replace all = %s", s.replaceAll("\\s+", ""));
// Example where it uses the most.
String s = "My name is yashwanth . M";
String s2 = "My nameis yashwanth.M";
System.out.println("Normal : "+s.equals(s2));
System.out.println("Replace : "+s.replaceAll("\\s+", "").equals(s2.replaceAll("\\s+", "")));
}
如果字符串只包含单个空格,则replace()将不替换,
如果空格多于一个,则执行replace()操作并删除空格。
public static String singleSpace(String str){
return str.replaceAll(" +| +|\t|\r|\n","");
}
计算字符串中的空格数。
public static String spaceCount(String str){
int i = 0;
while(str.indexOf(" ") > -1){
//str = str.replaceFirst(" ", ""+(i++));
str = str.replaceFirst(Pattern.quote(" "), ""+(i++));
}
return str;
}
pattern .quote("?")返回字面模式字符串。
如果你已经在你的项目中使用了Guava(19+节),你可能想要使用这个:
CharMatcher.whitespace().trimAndCollapseFrom(input, ' ');
或者,如果你需要删除空格符号(或U+0020,查看更多空白),使用:
CharMatcher.anyOf(" ").trimAndCollapseFrom(input, ' ');
试试这个:
String after = before.trim().replaceAll(" +", " ");
另请参阅
String.trim () 返回字符串的副本,省略前导和尾随空格。 regular-expressions.info /重复
没有trim()正则表达式
只用一个replaceAll也可以做到这一点,但这比trim()解决方案可读性差得多。尽管如此,这里提供它只是为了展示regex可以做什么:
String[] tests = {
" x ", // [x]
" 1 2 3 ", // [1 2 3]
"", // []
" ", // []
};
for (String test : tests) {
System.out.format("[%s]%n",
test.replaceAll("^ +| +$|( )+", "$1")
);
}
有三名候补:
^_+:字符串开头的任意空格序列 匹配并替换为$1,它捕获空字符串 _+$:字符串结尾的任意空格序列 匹配并替换为$1,它捕获空字符串 (_)+:不匹配上述任何一个空格序列,表示它在中间 匹配并替换为$1,它捕获单个空格
另请参阅
regular-expressions.info /锚
你也可以使用环视。
test.replaceAll("^ +| +$|(?<= ) ", "");
OR
test.replaceAll("^ +| +$| (?= )", "")
< >空间(?=)匹配一个空格字符,后面跟着另一个空格字符。所以在连续的空格中,它会匹配除最后一个空格以外的所有空格,因为它后面没有空格字符。这将为删除操作后的连续空格留下一个单独的空格。
例子:
String[] tests = {
" x ", // [x]
" 1 2 3 ", // [1 2 3]
"", // []
" ", // []
};
for (String test : tests) {
System.out.format("[%s]%n",
test.replaceAll("^ +| +$| (?= )", "")
);
}
String blogName = "how to do in java . com";
String nameWithProperSpacing = blogName.replaceAll("\\\s+", " ");