java中如何去掉
去掉字符串中的空格
使用 trim() 方法可以去掉字符串两端的空格:
String str = " Hello World ";
String trimmedStr = str.trim();
System.out.println(trimmedStr); // 输出 "Hello World"
去掉字符串中的所有空格
使用 replaceAll() 方法结合正则表达式去掉所有空格:

String str = " Hello World ";
String noSpaceStr = str.replaceAll("\\s", "");
System.out.println(noSpaceStr); // 输出 "HelloWorld"
去掉字符串中的特定字符
使用 replace() 方法去掉字符串中的特定字符:

String str = "Hello-World";
String newStr = str.replace("-", "");
System.out.println(newStr); // 输出 "HelloWorld"
去掉字符串中的换行符
使用 replaceAll() 方法去掉字符串中的换行符:
String str = "Hello\nWorld";
String noNewlineStr = str.replaceAll("\\n", "");
System.out.println(noNewlineStr); // 输出 "HelloWorld"
去掉字符串中的重复字符
使用循环或流处理去掉字符串中的重复字符:
String str = "aabbcc";
String distinctChars = str.chars()
.distinct()
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
System.out.println(distinctChars); // 输出 "abc"






