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

Which characters have been removed by string.trim () in Java?

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

Share

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

This article is about which characters have been removed from string.trim () in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What exactly does the String.Trim () method do for us, just removing spaces at both ends of the string?

Which characters are removed by the trim () method? How does trim () / substring () return a string object?

Which characters are removed by the trim () method?

See a problem on Stack Overflow (click here to see):

I am receiving a string from server trailing one or two lines of spaces like below given string.

String str = "abc* *"

Consider * as spaces after my string

I have tried a few methods like

Str = str.trim (); str = str.replace (String.valueOf ((char) 160), ") .trim (); str = str.replaceAll ("\ u00A0 ",")

But none is working. Why i am not able to remove the space?

To do this, I looked at the source code of the trim () method and found it interesting.

Take a look at the following code first:

Public static void main (String [] args) {String str = "abc"; System.out.println (str.length ()); str = str.trim (); System.out.println (str.length ());}

I don't know what your teacher (or book) said when you first learned Java. I started by watching teacher Bi Xiangdong's video, when Bi Xiangdong said that the trim () method would remove the spaces at both ends of the string; later, when watching "Java Core Technology", the book said, "return a new string. This string will delete the spaces at the head and tail of the original string." (the Chinese version of the 9th edition is on 51 pages and 20 lines in Volume 1. So I always take it for granted that the trim () method will trim the "spaces" at both ends of the string, which is why I find it interesting when I see the source code.

Among other things, the above code outputs 9, 3, that is, the "" at the end of the string is dropped by trim ().

More interestingly, if you copy the above code to Notepad++, it looks like this:

On Eclipse, it looks like this:

Yes, this special symbol can not be seen on Eclipse and does not take up any "space". If we accidentally mix such characters into the code, it will be very painful to make mistakes.

Let's get back to the point, and now that we know that the trim () method is not as simple as trim dropping spaces, what exactly is it missing from trim? We might as well take a look.

/ * * @ return A string whose value is this string, with any leading and trailing white * space removed, or this string if it has no leading or * trailing white space. * / public String trim () {int len = value.length; int st = 0; char [] val = value; / * avoid getfield opcode * / while ((st

< len) && (val[st] 0) || (len < value.length)) ? substring(st, len) : this; 也就是说,trim()方法实际上的行为并不是"去掉两端的空白字符",而是"截取中间的非空白字符"。 再看substring()方法: public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex >

Value.length) {throw new StringIndexOutOfBoundsException (endIndex);} int subLen = endIndex-beginIndex; if (subLen < 0) {throw new StringIndexOutOfBoundsException (subLen);} return ((beginIndex = = 0) & & (endIndex = = value.length)? This: new String (value, beginIndex, subLen);}

We see that when the "intercept" action actually occurs, because it is impossible to declare a String constant directly here, that is, substring () cannot create an object directly in the constant pool as we do when we write String str = "abc", so it returns an object that comes out of new, which is in Heap memory.

Thank you for reading! This is the end of the article on "which characters have been removed from string.trim () in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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