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 before and after a string by Java

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

Share

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

This article mainly introduces the relevant knowledge of "how Java removes the spaces before and after the string". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to remove the spaces before and after the string in Java" can help you solve the problem.

Problem description:

Java removes the white space before and after the string. As a young man, I've been doing it for most of the day, but it's actually quite simple.

Solution: way 1:

Trim () trim () is a common method in String to return a copy of the string, ignoring leading and trailing whitespace *

Method 2: regular expression:

ReplaceAll (String regex,String replacement): is also a common method of the String class

Is a parameter that can be filled with a regular expression, and the second parameter is a substitute

The regular formula needed here:

(1) s represents a space

(2) ^ indicates the beginning

(3) $indicates the end

(4) + indicates quantity

Add: JAVA regular expressions match multiple spaces

Demand

In order to solve the problem of multiple spaces caused by the tab key, sometimes we have to cut a row of data with spaces. If there are multiple spaces, the cutting spaces will appear. We want to remove all the spaces, so we need to use some methods.

Solution

Use regular expressions to match spaces

\ s +

First of all, use the split ("\ s +"); method to cut the string and match spaces as much as possible, which is also interesting, because the number of spaces is different, and the number of matched spaces can be changed dynamically. This implementation principle can look at the underlying principle, which is interesting.

Test:

String string= "a b an a"; for (String a:string.split ("\ s +")) {System.out.println (a);} expand knowledge

The () [] {} of a regular expression has different meanings.

() is to extract the matching string. If there are several () in the expression, there are several matching strings. (s*) A string that represents consecutive spaces.

[] is the range of characters that define a match. For example, [a-zA-Z0-9] means that the characters in the corresponding position should match English characters and numbers. [s*] indicates a space or a * sign.

{} is generally used to indicate the length of a match, for example, s {3} means to match three spaces, and s {1 # 3} means to match one to three spaces.

(0-9) match'0-9 'itself. [0-9] * matching numbers (note that there is a *, which can be empty) [0-9] + matching numbers (note that there is a + after it, which cannot be empty) {1-9} spelling error.

[0-9] {0prit 9} represents a numeric string with a length of 0 to 9

There is an essential difference between () and [].

The content in () represents a subexpression. () itself does not match anything, nor does it restrict matching anything, but treats the contents in parentheses as the same expression.

For example: (ab) {1 ab 3}, it means that ab appears together at least once in a row, at most 3 times. If there are no parentheses, ab {1J 3} means a, followed by b at least once and at most 3 times. In addition, parentheses are also important in matching patterns. This will not extend. LZ can check it out for himself if he is interested.

[] indicates that the matching character is in [] and can only appear once, and that special characters written in [] will be matched as normal characters. For example, [(a)] matches (, a,), these three characters.

So () [] whether it is the meaning of function or expression, there is a big difference, and there is no connection.

This is the end of the introduction to "how Java removes spaces before and after a string". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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