简体   繁体   English

仅替换字符串中的一个指定字符

[英]Replace only one specified character from string

I want to replace one character at each iteration from a string.我想在每次迭代时从字符串中替换一个字符。 I already wrote some code that produces the following output:我已经编写了一些生成以下 output 的代码:

est美东时间

st英石

t

This is fine and my expected result.这很好,也是我预期的结果。


But when I add the input: "hello", my output changes:但是当我添加输入:“你好”时,我的 output 发生了变化:

ello你好

llo llo

o o

As you can see, the two "l"'s get removed at the same time.如您所见,两个“l”同时被删除。 The reason is the following:原因如下:

"replace(CharSequence target, CharSequence replacement) Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence". “replace(CharSequence target, CharSequence replacement) 用指定的文字替换序列替换此字符串中与文字目标序列匹配的每个 substring”。

As you can see from the documentation, each substring gets replaced.从文档中可以看出,每个 substring 都被替换了。 Is there an alternative function in the String class?字符串class中是否有替代的function? Note that I only want to use String and no other third party library.请注意,我只想使用 String 而不是其他第三方库。


Maybe the replace-first function does achieve that?也许先替换 function 确实实现了这一点? But since it requires regex I don't know how to use it;(但是因为它需要正则表达式我不知道如何使用它;(

Thanks for any help in advance!提前感谢您的帮助!

Here is my code:这是我的代码:

public static boolean isAnagram(String first, String second){

    final int first_len = first.length();
    final int second_len = second.length();
    if (first_len != second_len){
        return false;
    }

    //String newWord = oldWord.replace(oldChar,newChar)

    for (int i = 0; i < first_len; i++) {
        char ch = first.charAt(i);
        first = first.replace(ch, ' ');
        System.out.println(first);
        for (int j = 0; j < first_len; j++){
            if (second.charAt(j) == ch) {
                second = second.replace(second.charAt(j), ' ');
            }
        }
    }

    if (first.trim().isEmpty() && second.trim().isEmpty()){
        return true;
    }
    return false;
}

Simply using "Hello".substring(1);只需使用"Hello".substring(1); will remove the first letter in a consistent manner将以一致的方式删除第一个字母

While I do think there are better ways to check if a String is an anagram, I think it might be helpful if you see how your idea implemented correctly would look like.虽然我确实认为有更好的方法来检查字符串是否是变位词,但我认为如果您了解您的想法正确实施的样子可能会有所帮助。

Your method was needlessly complicated.你的方法不必要地复杂。 All you really need to do is:您真正需要做的是:

  1. Iterate over the chars of your first String遍历第一个字符串的字符
  2. replace the first occurrence of that char in the second string with an empty String.用空字符串替换第二个字符串中该字符的第一次出现。
  3. After you are done iterating check if the second String is now empty, which will tell you if all chars from the first String where found:完成迭代后,检查第二个字符串现在是否为空,这将告诉您是否找到了第一个字符串中的所有字符:

-- --

public static boolean isAnagram(String first, String second) {

    final int first_len = first.length();
    final int second_len = second.length();
    if (first_len != second_len) {
        return false;
    }
    
    // If you want your anagram check to be case sensitive, comment out the following 2 lines
    first = first.toLowerCase();
    second = second.toLowerCase();
    
    for (int i = 0; i < first_len; i++) {
        final char ch = first.charAt(i);
        second = second.replaceFirst(Pattern.quote(String.valueOf(ch)), "");
    }

    if (second.trim().isEmpty()) {
        return true;
    }
    return false;
}

Because the replaceFirst method uses regular expressions as an argument we use the method Pattern.quote to make sure the passed character gets interpreted as a literal character and not accidentally as some special regular expression character.因为replaceFirst方法使用正则表达式作为参数,所以我们使用方法Pattern.quote来确保传递的字符被解释为文字字符,而不是意外地被解释为一些特殊的正则表达式字符。

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

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