1

I'm having problems with strings and I need a solution I'm trying to replace characters found at a certain position with a character found in also the same position for example

 private String wordNormalize(String enteredWord,String dictionary){
    String normalizedWord = null;
// remove empty spaces at beginning and at the end of the word and change to lower case
    normalizedWord = enteredWord.trim().toLowerCase();

    //normalize term, removing all punctuation marks

    normalizedWord = normalizedWord.replaceAll("["+punctuationMarks2+"]", "[b,v]");

    //normalize word removing to character if dictionary has english lang                                           
    normalizedWord = normalizedWord.replaceFirst("to ", " ");
    //normalizeWord if dictionary has german
    if(normalizedWord.length() > 0){
        normalizedWord.replace("a,b,c","t,u,v");
    /*for(int i = 0;i<normalizedWord.length();i++){
          char currentChar = normalizedWord.charAt(i); // currently typed character
          String s1= Character.toString(currentChar);
        for(int j = 0;j<specialCharacters.length;j++){
        s1.replaceAll("[ "+specialCharacters[i]+" ]",""+replaceCharactersDe[i]+"");
        }
         = str.replace("a,b,c","t,u,v");
    }*/
    }

    //normalize term removing special characters and replacing them 
    /*for(int i = 0; i > specialCharacters.length;i++){
        if(normalizedWord.equals(specialCharacters[i])){
            normalizedWord = replaceCharactersDe[i];
        }
    }*/
    return normalizedWord;
}

So if a user enters a its replaced with t and if a user enters b its replaced with u and if the user enters c it will be replaced with v and only in that order is this possible and if it is show me the right way its supposed to be done

2
  • 1
    String.replace(char, char) Commented Nov 19, 2012 at 8:00
  • What do you mean by only in that order? Commented Nov 19, 2012 at 8:01

1 Answer 1

1

It is not clear to me what you are trying to approach with

normalizedWord = normalizedWord.replaceAll("["+punctuationMarks2+"]", "[b,v]");

It does not seem right, but i don't know how to fix it because I don't know what it's trying to do. I guess what you are looking for is

normalizedWord = normalizedWord.replaceAll("\\p{Punct}", "");

On the other part you are doing nothing, because Strings are immutable. You want to do something like

normalizedWord = normalizedWord.replace("a,b,c","t,u,v");

but that would replace all occurrences of the substring "a,b,c" with the string "t,u,v"-

What you want is:

normalizedWord = normalizedWord.replace('a', 't');
normalizedWord = normalizedWord.replace('b', 'u');
normalizedWord = normalizedWord.replace('c', 'v');

We could work on a more general solution, but you have to show us how the dictionary , which is a String, is formatted.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry i wasn't clear forget this part normalizedWord = normalizedWord.replaceAll("["+punctuationMarks2+"]", "[b,v]"); because i already have this under control the last part is the real problem but it seems according to your answer its not possible other than the answer you've given me is there any other way it can be done without repeating the replace method for each character i want replaced
sure, put it in a for loop and replace the first char with the one to be replaced and the second one with the replacement char

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.