简体   繁体   English

在Java中,如何用字符串替换任意字符?

[英]How do you replace arbitrary characters with a string in Java?

I'm trying to create a method that replaces an individual character in a string with another string so far, these are my parameters. 到目前为止,我正在尝试创建一种方法,该方法将一个字符串中的单个字符替换为另一个字符串,这些是我的参数。

public static String replaceAt(String str, int position, String replacement) 公共静态字符串replaceAt(String str,int position,字符串替换)

It's supposed to look something like this when it runs correctly: 它正确运行时应该看起来像这样:

method         input                          output

replaceAt     "Hello, World", 5, " comma"     Hello comma World

This is what I have so far. 到目前为止,这就是我所拥有的。

{
   String result = "";

  char character = str.charAt(position);

  str = str.replace(character, replacement);

  result = str;

  return result;
 }

but I'm pretty sure I'm messing up the syntax somehow, any pointers on how to proceed? 但我很确定自己会以某种方式弄乱语法,有关如何进行操作的任何指示?

If you can use StringBuilder , then you could instantiate one with your input str and then call StringBuilder#replace(int, int, String) like 如果可以使用StringBuilder ,则可以使用输入str实例化一个,然后调用StringBuilder#replace(int, int, String)例如

public static String replaceAt(String str, int position, String replacement) {
    return new StringBuilder(str).replace(position, position + 1, 
            replacement).toString();
}

If you can't use that, then you might use String#substring(int, int) and concatenate your output like 如果不能使用它,则可以使用String#substring(int, int)并连接输出,例如

return str.substring(0, position) + replacement + str.substring(position + 1);

Note that internally uses StringBuilder to perform the concatenation (at least in recent Java releases), so it is equivalent to 请注意 ,内部使用StringBuilder来执行串联(至少在最近的Java版本中),因此它等效于

return new StringBuilder(str.substring(0, position)).append(replacement)
        .append(str.substring(position + 1)).toString();

(in very old releases StringBuffer was used). (在很旧的版本中,使用StringBuffer )。

The biggest problem with using replace , or other solutions that search for regexes, is that we don't know whether the character at that position is the only one in the string. 使用replace或其他搜索正则表达式的解决方案的最大问题是,我们不知道该位置的字符是否是字符串中的唯一字符。 Suppose the input string is "Hello, world, good morning" . 假设输入字符串是"Hello, world, good morning" Then your replaceAt would return "Hello comma world comma good morning" . 然后,您的replaceAt将返回"Hello comma world comma good morning" Maybe that's what you really want (it isn't clear), but a method that pretends to be one that "replaces" the character "at" the given "position" with a string, shouldn't do more than that. 也许这就是您真正想要的(尚不清楚),但是一种伪装成用字符串“替换”给定“位置”处的字符的方法,不应该做更多的事情。 (If that's really what you want, I recommend changing the name and the parameters.) (如果这确实是您想要的,我建议更改名称和参数。)

Assuming you want to replace the character at that position, and only the character at that position, I'd recommend using substring instead: 假设您要替换该位置的字符,并且替换该位置的字符,我建议改为使用substring

public static String replaceAt(String str, int position, String replacement) {
    return str.substring(0, position) + replacement + str.substring(position + 1);
}

Edit: If you want to handle the case where position == the string length, so that there is no character at that position: 编辑:如果要处理position ==字符串长度的情况,那么在该位置没有字符:

public static String replaceAt(String str, int position, String replacement) {
    return str.substring(0, position) + replacement + (position == str.length() ? "" : str.substring(position + 1));
}

(If position > the string length, the first substring won't work. Depending on the use case, you can let it throw an exception if this case isn't supposed to happen. Or you could do this:) (如果position >字符串长度,则第一substring将不起作用。根据使用情况,如果不希望发生这种情况,可以让它抛出异常。或者您可以这样做:)

public static String replaceAt(String str, int position, String replacement) {
    return str.substring(0, Math.min(position, str.length())) + replacement + (position >= str.length() ? "" : str.substring(position + 1));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM