如何在Java字符串中删除字符之间的多余空格

huangapple go评论72阅读模式
英文:

How do I remove excessive spaces between characters in java String

问题

我正在从一个文件中提取文本,但由于某种原因,它的格式在每个字符之间都有额外的空格(例如:“H i , I a m a S t r i n g .”)。

是否有办法可以干净地移除多余的空格,类似于以下示例代码:

public String cleanString(String string){
    //执行某些操作
    return cleanedString;
}
String oldString = "1  :  H i ,  I ' a m  a  S t r i n g .";

System.out.println(cleanString(oldString));

输出: "1: Hi, I'am a String."

编辑:通过“excessive spaces/white space”,我指的是字符串中每个字符之间的额外空格,包括有效的空格。

我正在用•替换“excessive space”,以便更容易看到:
字符串 -> "H•e•l•l•o• •W•o•r•l•d"
想象一下,•就是普通的空格或者"space"字符。我想要去掉它们。

这些额外的空格确实是空格,而不是空字符,我已经仔细检查过了。

英文:

I am pulling text from a file and for some reason it is formatted with extra whitespace between each character(ex: "H i , I a m a S t r i n g .").

Is there a way I can cleanly remove the excess white space something like

public String cleanString(Sting string){
    //do Something
    return cleanedString;
}
String oldString = "1  :  H i ,  I ' a m  a  S t r i n g .";

System.out.println(cleanString(oldString));

Output: "1: Hi, I'am a String."

Edit: By excessive spaces/white space I mean the extra space between every character in the string including the valid spaces.

I am replacing the "excessive space" with • so it is more visible
String -> "H•e•l•l•o• •W•o•r•l•d"
Imagine that the • is just white space or the "space" character. I would like to remove them.

The extra spaces are indeed spaces and not null characters, I double checked that.

答案1

得分: 1

我们需要用第二个符号(not a space)替代space,not a space序列。

在单词之间有多个空格,单词中的字母之间只有一个空格。使用正则表达式oldString.replaceAll(" ([^ ]+)", "$1"),我们去除了任何后面跟随非空格字符的空格。这样,只有单词之间有空格,单词中的字母之间没有空格。现在,我们可以使用以下表达式规范单词之间的空格:newString1.replaceAll("[ ]{2,}", " ")

以下是执行此操作的代码:

@Test
void removeEmptySpaces() {
  final String oldString = "1  :  H i ,  I ' a m  a     S t r i n g .";
  final String expectedString = "1 : Hi, I'am a String.";
  // 尝试用第二个符号替代所有'space,not a space'序列
  final String newString1 = oldString.replaceAll(" ([^ ]+)", "$1");
  final String newString = newString1.replaceAll("[ ]{2,}", " ");
  System.out.println(newString); // 1 : Hi, I'am a String.
  Assertions.assertEquals(expectedString, newString);
}

希望这能满足您的需求。

英文:

We have to substitute space,not a space sequence with the second symbol(s) ( not a space ).

There are more than one spaces between the words and one space between the letters in the word. With the regular expression oldString.replaceAll(" ([^ ]+)", "$1") we are removing any space that is followed by non space character. In that way only spaces is left between words and no spaces left between the letters in the word. Now we can normalize the spaces between words with the following expression: newString1.replaceAll("[ ]{2,}", " ")

Below is the code that doing it:

  @Test
  void removeEmptySpaces() {
    final String oldString = "1  :  H i ,  I ' a m  a     S t r i n g .";
    final String expectedString = "1 : Hi, I'am a String.";
    // Trying to substitute all 'space,not a space' sequence with the second symbol(s)
    final String newString1 = oldString.replaceAll(" ([^ ]+)", "$1");
    final String newString = newString1.replaceAll("[ ]{2,}", " ");
    System.out.println(newString); // 1 : Hi, I'am a String.
    Assertions.assertEquals(expectedString, newString);
  }

  }

huangapple
  • 本文由 发表于 2023年3月7日 23:32:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定