如何在 Java 中去除字符串中的所有空格?

java 8object oriented programmingprogramming

String 类的 replaceAll() 方法会将此字符串中与给定正则表达式匹配的每个子字符串替换为给定的替换值。您可以通过将"" " 替换为 """ 来去除字符串中的所有空格。

示例

import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str = new String("Hi how are you Welcome to Tutorialspoint.com");
      System.out.print("Return Value :" );
      System.out.println(Str.replaceAll(" ", ""));
   }
}

输出

Return Value :HihowareyouWelcometoTutorialspoint.com

相关文章