cool hit counter The difference between the string replacement functions replace and replaceAll in Java_Intefrankly

The difference between the string replacement functions replace and replaceAll in Java


background

I crawled the site again in my spare time, and this time I ran into a bit of a problem, the data I crawled down was a bit odd. It's in xml format, but there's some weird stuff, and every now and then one comes along, and the valid data is wrapped in and . I've taken a section and posted it here: (simple formatting for easy viewing)

How to parse this data is not difficult, Jsoup easy to handle, as for how to use Jsoup, not the focus of this article, here not to introduce.

When Jsoup parses the data, it can only parse to the tag, for example, the title above, parsed by Jsoup is.

As already mentioned, the valid data is the part sandwiched between and. If it's just a few strings, then you can easily get the valid data by string interception methods as well. But what is posted above is only partial, the actual whole data is a list with a lot of and , and it would undoubtedly be tedious to intercept the strings one by one.

So here's what I did: first I did a string replacement of the entire data, replacing the and with the empty string; then I handed it off to Jsoup to parse, so that it's a straightforward one-step loop.

String Replacement

Based on the above idea, it becomes crucial to replace all the sums at once. The good thing is that the String class has a replacement method: and, so we can just take it and use it.

But I got confused here by the names of the two methods, thinking it was replacing all of the and Well, that's fitting. And so it was used.

As soon as it was written out this way, AS was prompting for a problem with the parameter:.

This was confusing, then honestly read the instructions for replaceAll:.

Replaces each substring of this string that matches the given regular expression with the given replacement.

replaceAll is implemented as.

The meaning is obvious, the first argument regex is a regular expression, and the function of this method is to use replacement to Replace all content that satisfies the regex . And is also a key character in regular expressions, so the parameter problem occurs here when used directly without processing.

I don't know much about regular expressions here, so I didn't delve into the replaceAll method, but instead looked at the description and implementation of replace.

The meaning here is to replace every string that satisfies the condition. And the substitution process is one-way from the beginning of the string to the end, without regression; for example: using substitution inside, the result is, instead. This means that replace and replaceAll are not literal, they are All replace all content that meets the condition

Also, the implementation of replace here is very rigorous, and the case for is handled by adding replacement at the beginning and end of the original string and between all its characters. As an example.

The result.

summary

1. replace and replaceAll both replace all content that meets the conditions, but replace's match condition is a normal string, while replaceAll's match condition is a regular expression.

2, when the string to be processed is relatively large, the efficiency of replaceAll is always faster than replace; when the string is not large, the efficiency of the two is comparable.

3, when the string can not determine whether the escape character, and also do not need to escape, it is recommended to use the replace function; otherwise, use the replaceAll function.


Recommended>>
1、Why do all startups choose WeChat public development in the early stages of their business
2、Just now Ministry of Industry and Information Technology Announces 2018 Innovation Projects for Deep Integration of Artificial Intelligence and Real Economy
3、From Zero to Successful Problem Solving with 0ctfezdoor
4、Blockchain technology that redefines the world Tencent has quietly acted starting with the registered trademark
5、2018 Smart Logistics Industry Development Trend TOP Summit Huaxun Weishi Little Secret Product Launch Successfully Held

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号