Get the App
SLTechnology News&Howtos  ›  Development  › 

How to remove spaces in a string by Java

Shulou Source: shulou.com Published: 2022-06-02 12:56:57 11月13日 Update

This article mainly introduces "how to remove spaces in strings by Java". In daily operation, I believe many people have doubts about how to remove spaces in strings by Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to remove spaces in strings by Java". Next, please follow the editor to study!

1. Method classification

Str.trim (); / remove leading and trailing spaces str.replace (","); / remove all spaces, including leading and trailing, middle str.replaceAll ("", "); / / remove all spaces, including leading and trailing, intermediate str.replaceAll (" + ","); / / remove all spaces, including leading and trailing, intermediate str.replaceAll ("\\ s * ",") / / most white space characters can be replaced, not limited to spaces

\\ s* can match any of the white space characters such as spaces, tabs, page breaks, and so on.

2. Code example

Package test;public class Test {/ * description: method for removing spaces in strings * / public static void main (String [] args) {String str = "hi world ~"; / / method 1:str.trim () System.out.println ("1muri->" + str.trim ()); / / method 2:str.repalce ("", ") System.out.println (" 2murf-> "+ str.replace (",")) / / method 3:str.repalceAll (",") System.out.println ("3murmuri->" + str.replaceAll ("", "); / / method 4:str.repalceAll (" + ",") System.out.println ("4Mutual->" + str.replaceAll ("+", ") / / method 5:str.repalceAll ("\\ s *", ") System.out.println (" 4Mutual-> "+ str.replaceAll ("\\ s * ","));}}

3. Output result

4. Replace and replaceAll are commonly used methods to replace characters in JAVA. The differences between them are:

(1) the parameters of replace are char and CharSequence, which can support both character substitution and string substitution (CharSequence means string sequence, to put it bluntly, string)

(2) the parameter of replaceAll is regex, which is based on the substitution of regular expressions. For example, all numeric characters in a string can be replaced with asterisks by replaceAll ("\\ d", "*").

Similarities: all are replaced, that is, a character or string in the source string is replaced with a specified character or string. If you only want to replace what appears for the first time, you can use the

ReplaceFirst (), which is also based on the substitution of regular expressions, but unlike replaceAll (), replaces only the string that occurs for the first time

At this point, the study of "how to remove spaces in a string by Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Characters strings methods spaces head and tail learning parameters more white space first time expressions rules help support different practical same and then to put it bluntly that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info Microsoft Huawei Apple