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

What are the precautions when using replaceFirst?

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

Share

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

This article mainly introduces the matters needing attention when using replaceFirst, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.

Matters needing attention when using replaceFirst

Recently, the company's project has a need to develop and fill in the blanks. After consideration and analysis, it is decided to implement it through SpannableString. In this process,

ReplaceFirst gave me two tricks:

Kengichi

Underline the blank position through UnderlineSpan, and set listening to the blank place through ClickableSpan. Click on the glide line to exhale the dialog box with input box and replace it with the blank.

Since UnderlineSpan cannot underline spaces on some mobile phones, add a pair of parentheses at the beginning and end of the blank position.

There is a problem assigning the input value to the empty space

For example:

Content = I am # # person, I love # #; firstValue = China; after the following code:

Content = content.replaceFirst (YHAnswer.SEPARATOR, "(" + fisrtValue+ ")")

Content becomes me (Chinese, I love # #); the expected value is that I am (Chinese), I love # #

I don't know if this is a bug. I studied it for a long time. I thought it was caused by parentheses, and it was the same with other symbols and even Chinese characters, but it was normal not to add parentheses.

Finally, it is found that the reason is that the latter parameter is spliced, so it will be no problem if it is changed to the following.

String before = "(" + fisrtValue+ ")"; content = content.replaceFirst (YHAnswer.SEPARATOR, before); pit two

In the input process, it is inevitable to enter a special symbol. When entering the $sign, the $sign will disappear after replaceFirst.

For example:

Content = I am # # person, I love # #; firstValue = China $$country; after the following code:

String before = "(" + fisrtValue+ ")"; content = content.replaceFirst (YHAnswer.SEPARATOR, before)

Content becomes that I am (Chinese), I love # #); the expected value is that I am (China), I love # #

If the requirement allows, you can block $directly, and if not, you need to add escape characters.

The reason why the replaceFirst method of the String class is not easy to use

Today, I happened to replace the following AND with String's replace method. I found that it didn't work any way, so I made an experiment.

Code 1: String str = new String ("AND AAE112"); str.replaceFirst ("AND", "); System.out.println (str); AND AAE112 Code 2: string str = new String (" AND AAE112 "); String out = str.replaceFirst (" AND ","); System.out.println (out); AAE112

As you can see, the result of Code 2 is correct, and you can see that replaceAll does not change the original str, which is different from PB and PL/SQL. The reason should be that str points to an address.

Thank you for reading this article carefully. I hope the article "what are the points for attention when using replaceFirst" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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