Home > Net >  Replace only one specified character from string
Replace only one specified character from string

Time:11-29

I want to replace one character at each iteration from a string. I already wrote some code that produces the following output:

est

st

t

This is fine and my expected result.


But when I add the input: "hello", my output changes:

ello

llo

o

As you can see, the two "l"'s get removed at the same time. 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".

As you can see from the documentation, each substring gets replaced. Is there an alternative function in the String class? Note that I only want to use String and no other third party library.


Maybe the replace-first function does achieve that? 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;
}

CodePudding user response:

Simply using "Hello".substring(1); will remove the first letter in a consistent manner

CodePudding user response:

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.

  • Related