是否要用StringBuilder替换所有出现的字符串?
我错过了什么吗? 或者,StringBuilder是否缺少与常规String类相同的“用字符串b替换所有出现的字符串a”函数? StringBuilder的替换功能不完全相同。 有没有更有效地使用常规String类生成多个字符串的方法?
gar sh0 pasked 2019-06-19t 10336055336012 z
13个解决方案
69电压
那么,我可以写循环:
publicstaticvoidreplaceall (stringbuilder builder,String from,String to ) )。
{
intindex=builder.index of (从);
wile (索引!=-1 )
{
builder.replace(index,index from.length ),to );
index =to.length (; //Move to the end of the replacement
索引=builder.index of (from,index );
}
}
请注意,在某些情况下,使用lastIndexOf更快,可以从后面工作。 我认为用短字符串替换长字符串是这样的。 所以狂野的花卷开始的时候,任何置换都要少复制。 无论如何,这应该给你一个起点。
jonskeetanswered 2019-06-19t 10336055336033 z
33电压
也可以使用Pattern/Matcher。 来自Matcher javadocs :
patternp=pattern.compile('cat );
matcherm=p.matcher (onecattwocatsintheyard );
StringBuffer sb=new StringBuffer (;
wile(m.find ) ) }
m .应用替换(sb,' dog );
}
m.appendtail(sb;
system.out.println(sb.tostring ();
ronromeroanswered 2019-06-19t 10336055336058 z
12电压
要查看String类的替换所有方法的JavaDoc,请执行以下操作:
将字符串中与指定正则表达式匹配的每个子字符串表达式替换为指定的替换。 调用此方法str.replaceall(regex,repl )的结果在表示形式上完全相同
Java.util.regex.pattern.com pile (正则表达式).matcher(str ).replace all (repl ) ) ) ) ) ) ) ) )。
如您所见,可以使用Pattern和Matcher完成此操作。
alfredoosorioanswered 2019-06-19t 10336056336044 z
11电压
Apache Commons Lang中的org.Apache.com mons.lang3. text.str builder类允许替换。
publicstrbuilderreplaceall (字符串搜索str,字符串复制器)。
*这不是正则表达式,而是简单的字符串。
paulvargasanswered 2019-06-19t 10336057336017 z
11电压
@Adam :在你的代码片段中,我认为应该跟踪m.find ) )的开始位置。 因为字符串替换可能会在最后一个字符匹配后更改偏移。
publicstaticvoidreplaceall (stringbuilder sb、Pattern pattern、String replacement ) {
matcherm=pattern.matcher(sb;
int start=0;
wile (m.find (开始) ) }
sb.replace(m.start )、m.end )、replacement );
start=m.start () replacement.length );
}
}
fir 99 answered 2019-06-19t 10336057336043 z
5电压
即使是简单的东西也要使用String ReplaceAll函数本身。 你可以写那个
stringbuilder sb=new stringbuilder (' hithere,are you th
ere?")System.out.println(Pattern.compile("there").matcher(sb).replaceAll("niru"));
user2995215 answered 2019-06-19T10:58:09Z
2 votes
java.util.regex.Pattern.matcher(CharSequence s)可以使用StringBuilder作为参数,因此您可以使用start()和end()查找和替换模式的每个出现,而无需调用builder.toString()
Pierre answered 2019-06-19T10:58:34Z
2 votes
使用以下内容:
/**
* 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);
}
}
Anandan answered 2019-06-19T10:58:54Z
1 votes
这是一个原位replaceAll,它将修改传入的StringBuilder。 我以为我会发布这个,因为我想要替换所有没有创建一个新的String。
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 Gent answered 2019-06-19T10:59:35Z
1 votes
怎么样创建一个方法,让String.replaceAll为你做:
public static void replaceAll(StringBuilder sb, String regex, String replacement)
{
String aux = sb.toString();
aux = aux.replaceAll(regex, replacement);
sb.setLength(0);
sb.append(aux);
}
Christian answered 2019-06-19T11:00:00Z
0 votes
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();
}
ramesh answered 2019-06-19T11:00:20Z
0 votes
我找到了这个方法:Matcher.replaceAll(String replacement);在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();
}
Armysir answered 2019-06-19T11:00:47Z
0 votes
是。 使用String.replaceAll()方法非常简单:
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
Amitabha Roy answered 2019-06-19T11:01:17Z