![]() |
没有腹肌的开水瓶 · Exception in thread ...· 2 周前 · |
![]() |
英勇无比的莴苣 · Better error message ...· 2 天前 · |
![]() |
冷冷的胡萝卜 · SpringBoot学习笔记(八)——JWT ...· 昨天 · |
![]() |
爱热闹的葫芦 · Column.IsIdentity ...· 昨天 · |
![]() |
兴奋的玉米 · Mapping Attributes✨ | ...· 昨天 · |
![]() |
爱热闹的金鱼 · 恶意代码技术理论:宏病毒恶意代码分析 | ...· 3 月前 · |
![]() |
痴情的油条 · 字符串到LocalDateTime:常见问题 ...· 4 月前 · |
![]() |
聪明伶俐的铁板烧 · org.springframework.co ...· 4 月前 · |
![]() |
茫然的木瓜 · 设置动画和特效(围墙) | LYH个人技术展示· 5 月前 · |
我是否遗漏了什么,或者StringBuilder缺少与普通string类相同的“用string B替换字符串A的所有匹配项”功能?StringBuilder的替换功能并不完全相同。有没有什么方法可以在不使用普通的String类生成多个String的情况下更有效地做到这一点呢?
你可以写一个循环:
public static void replaceAll(StringBuilder builder, String from, String to) {
int index = builder.indexOf(from);
while (index != -1) {
builder.replace(index, index + from.length(), to);
index += to.length(); // Move to the end of the replacement
index = builder.indexOf(from, index);
}
请注意,在某些情况下,使用它可能会更快
,从后面工作。我怀疑如果你用一个短字符串替换一个长字符串,情况就是这样--所以当你开始的时候,任何替换字符串都有更少的需要复制的东西。无论如何,这应该给你一个起点。
使用以下内容:
/**
* Utility method to replace the string from StringBuilder.
* @param sb the StringBuilder object.
* @param toReplace the String that should be replaced.
* @param replacement the String that has to be replaced by.
public static void replaceString(StringBuilder sb,
String toReplace,
String replacement) {
int index = -1;
while ((index = sb.lastIndexOf(toReplace)) != -1) {
sb.replace(index, index + toReplace.length(), replacement);
}
这是一个将修改传入的StringBuilder的就地replaceAll。我想我应该发布这篇文章,因为我希望在不创建新字符串的情况下做replaceAll。
public static void replaceAll(StringBuilder sb, Pattern pattern, String replacement) {
Matcher m = pattern.matcher(sb);
while(m.find()) {
sb.replace(m.start(), m.end(), replacement);
}
我很震惊这样做的代码是如此简单(出于某种原因,我以为在使用匹配器时更改StringBuilder会抛出组开始/结束,但事实并非如此)。
这可能比其他正则表达式的答案更快,因为模式已经编译,您没有创建新的字符串,但我没有做任何基准测试。
@Adam:我认为在你的代码片段中,你应该跟踪m.find()的起始位置,因为字符串替换可能会改变最后一个字符匹配后的偏移量。
public static void replaceAll(StringBuilder sb, Pattern pattern, String replacement) {
Matcher m = pattern.matcher(sb);
int start = 0;
while (m.find(start)) {
sb.replace(m.start(), m.end(), replacement);
start = m.start() + replacement.length();
}
public static String replaceCharsNew(String replaceStr,Map replaceStrMap){
StringBuilder replaceStrBuilder = new StringBuilder(replaceStr);
Set keys=replaceStrMap.keySet();
for(String invalidChar:keys){
int index = -1;
while((index=replaceStrBuilder.indexOf(invalidChar,index)) !=-1){
replaceStrBuilder.replace(index,index+invalidChar.length(),replaceStrMap.get(invalidChar));
return replaceStrBuilder.toString();
}
我发现了这个方法:Matcher.replaceAll(字符串替换);在java.util.regex.Matcher.java中可以看到更多:
/**
* Replaces every subsequence of the input sequence that matches the
* pattern with the given replacement string.
* This method first resets this matcher. It then scans the input
* sequence looking for matches of the pattern. Characters that are not
* part of any match are appended directly to the result string; each match
* is replaced in the result by the replacement string. The replacement
* string may contain references to captured subsequences as in the {@link
* #appendReplacement appendReplacement} method.
* Note that backslashes (\) and dollar signs ($) in
* the replacement string may cause the results to be different than if it
* were being treated as a literal replacement string. Dollar signs may be
* treated as references to captured subsequences as described above, and
* backslashes are used to escape literal characters in the replacement
* string.
* Given the regular expression a*b, the input
* "aabfooaabfooabfoob", and the replacement string
* "-", an invocation of this method on a matcher for that
* expression would yield the string "-foo-foo-foo-".
* Invoking this method changes this matcher's state. If the matcher
* is to be used in further matching operations then it should first be
* reset.
* @param replacement
* The replacement string
* @return The string constructed by replacing each matching subsequence
* by the replacement string, substituting captured subsequences
* as needed
public String replaceAll(String replacement) {
reset();
StringBuffer buffer = new StringBuffer(input.length());
while (find()) {
appendReplacement(buffer, replacement);
return appendTail(buffer).toString();
}
是的。它使用起来非常简单
方法:
package com.test;
public class Replace {
public static void main(String[] args) {
String input = "Hello World";
input = input.replaceAll("o", "0");
System.out.println(input);
}
输出:
Hell0 W0rld
如果您真的想使用
取而代之的是,你可以这样做:
public static void main(String args[]) {
StringBuilder sb = new StringBuilder("This is a new StringBuilder");
System.out.println("Before: " + sb);
String from = "new";
![]() |
没有腹肌的开水瓶 · Exception in thread “main“ org.apache.spark.sql.AnalysisException: Cannot write incompatible data to 2 周前 |
![]() |
英勇无比的莴苣 · Better error message when "org.testng.internal.reflect.MethodMatcherException: Data provider mismat 2 天前 |
![]() |
爱热闹的金鱼 · 恶意代码技术理论:宏病毒恶意代码分析 | 云涯历险记 3 月前 |
![]() |
痴情的油条 · 字符串到LocalDateTime:常见问题与解决方案 4 月前 |
![]() |
聪明伶俐的铁板烧 · org.springframework.core.convert.ConverterNotFoundException: No converter found capable of convertin 4 月前 |
![]() |
茫然的木瓜 · 设置动画和特效(围墙) | LYH个人技术展示 5 月前 |
![]() |
很酷的蚂蚁 · java int数组转为16进制字符串-CSDN博客 10 月前 |