-1

I would like to ask you for your help, regarding this code. I am trying to do a kind of encoding of particular words, such as "Microsoft" etc. (random ones, just to learn the technique). I've suceeded in doing everything, but to make this kinf of searching for words case insesitive. Here is the code:

public class BannedWords {

public static String returnStars(int length){
    String stars = "";
    String addStar = "*";
    for (int i = 1; i<=length; i++){
        stars += addStar;
    }
    return stars;
}
public static void main(String[] args) {
    String textString = "Microsoft announced its next generation Java compiler today."
            + " It uses advanced parser and special optimizer for the Microsoft JVM.";
    StringBuilder text = new StringBuilder(textString);
    String bannedWords = "Java, JVM, Microsoft";

    String [] bWordsArr = bannedWords.split("[, ]+");

    for(int i = 0; i<bWordsArr.length; i++){
        int index = textString.indexOf(bWordsArr[i]);
        while(index != -1){
        text = text.replace(index, index+bWordsArr[i].length(), returnStars(bWordsArr[i].length()));
        index = textString.indexOf(bWordsArr[i], index +1);

        }
    }
        System.out.println(text.toString());
    }
}

I need to search for "Java", "JVM" and "Microsoft" words regardless of their case, even if we try "MiCrosoFt" it should work, but after a few hours thinking and trying to do it with using toUpperCase(), toLowerCase(), I couldn't find out how to do that. Do you have any ideas ?

Thank you beforehand ! :)

Kirev
  • 17
  • 1
  • 8
  • What did you do with `toUpperCase()` and `toLowerCase()` that didn't work? – awksp May 10 '14 at 16:14
  • `String.equalsIgnoreCase()` might be useful. – takendarkk May 10 '14 at 16:19
  • possible duplicate of [How to replace case-insensitive literal substrings in Java](http://stackoverflow.com/questions/5054995/how-to-replace-case-insensitive-literal-substrings-in-java) – Lie Ryan May 10 '14 at 16:33
  • @LieRyan It is not a duplicate, because I know how to deal with just one variable that I have to deal with. But here I have three of them, so in that case the situation is different and therefore I cannnot deal with it... – Kirev May 10 '14 at 17:01
  • is there something wrong with looping in Java? – Lie Ryan May 10 '14 at 17:02
  • @LieRyan Btw you start with the questions, so regarding to that, have you heard about the fact that String is immutable and everytime you change it as you sugested for my problem you would waste memory, because it would create new and new objects when a change is made while looping ? What do you think about that ? :) – Kirev May 10 '14 at 22:11

2 Answers2

0

When using indexOf(), toLowerCase() will convert checked text to lowercase. Then, you must put your search terms in lowercase.

String text = "Java is a good programming language.";
int index = text.toLowerCase().indexOf("java");

You can also use toUpperCase(), simply put your search terms in uppercase.

spongebob
  • 8,370
  • 15
  • 50
  • 83
0

Actually you should be using equalsIgnoreCase. This is the correct way of comparing strings irrespective of their case. And also you don't have to modify the original string to upper or lower case to perform check. I hope it helps :)

yourString.equalsIgnoreCase(anotherString)
vkg
  • 1,839
  • 14
  • 15
  • Thank you for your response. However this won't help in my situation as I am not comaring them, I just need to suppress the case-sensitivity. Thanks anyways, this might be of some help down the road. – Kirev May 10 '14 at 22:00