java Java中的字符串替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1949988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:40:57  来源:igfitidea点击:

String replace in Java

javaregexstring

提问by Aly

I currently have a string which contains the characters A, B and C for example the string looks like

我目前有一个包含字符 A、B 和 C 的字符串,例如字符串看起来像

"A some other random stuff B C"

the other random stuff doesn't contain A, B or C I want to replace the A, B & C with 'A', 'B' and 'C' respectively what's the best way to do that currently I'm doing:

其他随机内容不包含 A、B 或 CI 想要分别用“A”、“B”和“C”替换 A、B 和 C,目前我正在做的最佳方法是什么:

String.replace("A", "'A'").replace("B", "'B'").replace("C", "'C'")

回答by Mark Byers

cletus' answer works fine if A, B and C are those exact single characters, but not if they could be longer strings and you just called them A, B and C for example purposes. If they are longer strings you need to do:

如果 A、B 和 C 是那些确切的单个字符,则 cletus 的答案可以正常工作,但如果它们可以是更长的字符串并且您只是出于示例目的将它们称为 A、B 和 C,则不会。如果它们是更长的字符串,您需要执行以下操作:

String input = "FOO some other random stuff BAR BAZ";
String output = input.replaceAll("FOO|BAR|BAZ", "'
String input = "A some other random stuff B C";
String output = input.replaceAll("[ABC]", "'
'A' some other random stuff 'B' 'C'
'");
'");

You will also need to escape any special characters in FOO, BAR and BAZ so that they are not interpreted as special regular expression symbols.

您还需要对 FOO、BAR 和 BAZ 中的任何特殊字符进行转义,以便它们不会被解释为特殊的正则表达式符号。

回答by cletus

Use a regular expression:

使用正则表达式:

public static String replace(String string, String[] toFind, String[] toReplace) {
    if (toFind.length != toReplace.length) {
        throw new IllegalArgumentException("Arrays must be of the same length.");
    }
    for (int i = 0; i < toFind.length; i++) {
        string = string.replace(toFind[i], toReplace[i]);
    }
    return string;
}

Output:

输出:

public String quoteLetters(String s)
{
    return s.replace("A", "'A'").replace("B", "'B'").replace("C", "'C'");
}

回答by Pascal Thivent

Have a look at StringUtilsfrom Apache Commons Lang and its various replacemethods.

看看StringUtilsApache Commons Lang 及其各种replace方法。

回答by BalusC

I think regex is not really suitable if those are fairly complex strings.

如果这些字符串相当复杂,我认为正则表达式并不合适。

Consider just wrapping it in your own utility method taking arrays or lists as arguments.

考虑将它包装在您自己的实用程序方法中,将数组或列表作为参数。

##代码##

Maybe there's already one in the Apache Commons or Google Code. Give it a look if you prefer 3rd party API's above homegrown stuff.

也许 Apache Commons 或 Google Code 中已经有一个。如果您更喜欢 3rd 方 API 而不是自产的东西,请看一看。

Edit: why downvoting a technically correct but a in your opinion not preferredanswer? Just don't upvote.

编辑:为什么不赞成技术上正确但在您看来不是首选答案?只是不要点赞。

回答by Bill K

The other peoples answers are better, but under the assumption you are trying to learn, I wanted to point out the problems with your original code.

其他人的答案更好,但假设您正在尝试学习,我想指出您原始代码的问题。

Here would be a "Fixed" version of what you are trying to do (Using your style, which was close) and the things you need to do to make it work:

这将是您正在尝试做的事情的“固定”版本(使用您的风格,这是接近的)以及您需要做的事情才能使其发挥作用:

##代码##

Here are the problems you had:

以下是您遇到的问题:

  • Replace is a method, not a static function, so you have to call it on an instance of String, not String itself.
  • Replace returns an instance of the new string, it cannot modify the original string. In my code, I'm "Returning" the new string, s remains unmodified.
  • If this is homework, you should have tagged it as such.
  • Don't chain them like I did--the other answers are better because mine will actually create 3 new strings and then immediately destroy them.
  • Replace 是一种方法,而不是静态函数,因此您必须在 String 的实例上调用它,而不是 String 本身。
  • 替换返回新字符串的一个实例,它不能修改原始字符串。在我的代码中,我正在“返回”新字符串, s 保持不变。
  • 如果这是作业,您应该将其标记为作业。
  • 不要像我一样链接它们 - 其他答案更好,因为我的实际上会创建 3 个新字符串,然后立即销毁它们。