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 implement JAVA string substitution

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to achieve JAVA string substitution". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to achieve JAVA string substitution.

Recently, string substitution has been used in the development of java, which I thought was a simple problem, but it can't be solved. The string is obviously replaced, but it can not always output the correct result, very depressed, why on earth is it?

Let's take a look at my code first:

Newsdetialsnt= (Newsdetials) ntIter.next (); System.out.println ("News details" + nt); Stringcontent=nt.getContent (); System.out.println ("the content of the news is:" + content); / / convert the double quotation marks in the string to single quotation marks content.replaceAll ("\", "'"); System.out.println ("the replaced character pass is" + content)

Program explanation:

The first part of the program is to traverse the details of the news through an iterator.

My goal is to replace the single quotation marks in the string with double quotes.

The above is the method I started using, what is the result of my output? Maybe you'll feel the same way I did? The output is the replaced content.

The answer is wrong. The output is the same as before it was replaced. Obviously replaced, why is that? Me too. I haven't found the answer for a long time.

An accidental opportunity let me achieve my goal. My implementation is as follows:

Newsdetialsnt= (Newsdetials) ntIter.next (); System.out.println ("News details" + nt); Stringcontent=nt.getContent (); System.out.println ("the content of the news is:" + content); / / convert the double quotation marks in the string to single quotation marks Stringcontent0=content.replaceAll ("\", "'"); System.out.println ("the replaced character pass is" + content0)

Program explanation:

This code looks the same as the previous paragraph, but it achieves my goal.

I was so confused that I looked at the source code and found out the secret of it. Let's analyze the above two programs:

The difference between * is that the latter assigns the replaced string to another string variable, that is, the replaced string is taken out, while the former does not. It is this that leads to a difference in results.

So I looked at the source code of its implementation:

Let's look at the implementation of String's replaceAll () method:

PublicStringreplaceAll (Stringregex,Stringreplacement) {returnPattern.compile (regex) .matcher (this) .replaceAll (replacement);}

We can see that it is finally implemented by calling the matcher (this) .replaceAll (replacement) method, and let's see how it is implemented:

PublicStringreplaceAll (Stringreplacement) {reset (); booleanresult=find (); if (result) {StringBuffersb=newStringBuffer (); do {appendReplacement (sb,replacement); result=find ();} while (result); appendTail (sb); returnsb.toString ();} returntext.toString ();}

Looking at this code makes us understand everything:

We know that in java, if you are passing an object, it is an address pass, so let's assume that we can just replace it with the original string. At the same time, we call the alternative method of the string and directly modify the value of the string.

The above code tells us that it is wrong.

Because the bold part produces a new object: an instance of StringBuffer, and its string value is returned instead of the original content.

Thank you for your reading, the above is the content of "how to achieve JAVA string substitution". After the study of this article, I believe you have a deeper understanding of how to achieve JAVA string substitution, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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