![]() |
紧张的红金鱼 · 响应 CheckBox 单击 - ...· 1 年前 · |
![]() |
魁梧的机器人 · Apache Spark ...· 1 年前 · |
![]() |
痛苦的柑橘 · 微信小程序如何设置背景图片? | 微信开放社区· 1 年前 · |
![]() |
刀枪不入的乌冬面 · 详细解读XMLHttpRequest(一)同 ...· 1 年前 · |
我是否遗漏了什么,或者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";
![]() |
魁梧的机器人 · Apache Spark connector for SQL Server - Spark connector for SQL Server | Microsoft Learn 1 年前 |
![]() |
痛苦的柑橘 · 微信小程序如何设置背景图片? | 微信开放社区 1 年前 |