Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to remove spaces in a string by Java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report