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-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the relevant knowledge of "how to achieve java string substitution". 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 achieve java string substitution" can help you solve the problem.

String processing is very common in the daily work of programmers. It is common to see string-related API encapsulated in almost all languages. We just need to bring it over and use it directly. Take Java as an example. The String () class in jdk encapsulates almost all string-related operations, and the number of methods is nearly 100, which satisfies almost all string-related operations of programmers.

Because of this convenience, it is estimated that most Java programmers have never implemented string replace on their own. Here is the formal introduction of today's selected interview questions: do not rely on third-party libraries to implement a string replacement replace (String str, String target, String replacement) function, its function is to replace all target in str with replacement. In fact, this question does not involve any complex or advanced algorithms, you only need to master the basic programming to do it, but when I took this question out to interview a new graduate, his code stumbled. Later, I also continued to use this question to test several people, rarely write smoothly, I underestimated the difficulty of this question?

Problem-solving ideas

Back to the question itself, I would like to say a few more sentences, think carefully about this question is actually very simple, but this has stumped a large number of people, we still have to lay a good programming foundation before brushing the test questions. The idea of solving this problem is also very simple, we create a new StringBuilder, only need to add the part of str that is not target, if you encounter target, add the replacement string, there is really no complex algorithm is simply to test the basic skills of your programming, the code is as follows.

Public static String replace (String str, String target, String replacement) {/ / normal. Here we need to check the input of str,target,replacement. Here I omit. For example, str can directly return the empty string StringBuilder res = new StringBuilder (); for (int I = 0; I).

< str.length(); ) { if (isMatch(str, i, target)) { i += target.length(); // 如果匹配,需要直接向前跳target.length res.append(replacement); continue; } res.append(str.charAt(i++)); } return res.toString(); } // 单纯确认从str的pos位置开始,是否和target相匹配 private static boolean isMatch(String str, int pos, String target) { for (int i = 0; i < target.length() && i + pos < str.length(); i++) { if (str.charAt(i + pos) != target.charAt(i)) { return false; } } return true; } 看吧,代码其实没啥难度,但咋就好多明显刷过其他面试题的人都不会呢!!! Jdk中的replace实现 估计大多数人都没看过Jdk中的实现,所以顺带我们来欣赏下java String类中的replace方法是如何实现的。 public String replace(CharSequence target, CharSequence replacement) { String tgtStr = target.toString(); String replStr = replacement.toString(); int j = indexOf(tgtStr); if (j < 0) { return this; } int tgtLen = tgtStr.length(); int tgtLen1 = Math.max(tgtLen, 1); int thisLen = length(); int newLenHint = thisLen - tgtLen + replStr.length(); if (newLenHint < 0) { throw new OutOfMemoryError(); } StringBuilder sb = new StringBuilder(newLenHint); int i = 0; do { sb.append(this, i, j).append(replStr); // 先把未匹配字符添加进去,然后直接添加replStr i = j + tgtLen; } while (j < thisLen && (j = indexOf(tgtStr, j + tgtLen1)) >

0); / / find the next matching subscript return sb.append (this, I, thisLen). ToString ();}

The idea in jdk is the same as what we wrote above, but the code in jdk is more concise. In fact, jdk doesn't use anything sophisticated, but considers more data coding in indexOf ().

This is the end of the introduction on "how to implement java string substitution". 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: 266

*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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report