Home > Java > javaTutorial > Several ways to remove all spaces in a string in Java

Several ways to remove all spaces in a string in Java

高洛峰
Release: 2017-01-22 11:12:52
Original
2312 people have browsed it

Removing spaces in JAVA

1. String.trim()

trim() removes leading and trailing spaces

2.str.replace(" ", "" ); Remove all spaces, including the beginning, end and middle

String str = " hell o ";  
String str2 = str.replaceAll(" ", "");  
System.out.println(str2);
Copy after login

3. Or replaceAll(" +",""); Remove all spaces

4.str = .replaceAll("\\s *", "");
Can replace most whitespace characters, not limited to spaces
\s can match any of the whitespace characters such as spaces, tabs, form feeds, etc.

Update For related articles on several methods to remove all spaces in strings in Java, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template