0

I have a textView in which I can search some words and the code for the search is this:

public void btn_search(View view) {
    int total = 0;
    String word_search = Etxt.getText().toString().trim();
    String fullTxt = textView.getText().toString();
    String[] array = fullTxt.split("\n");
    String word;
    StringBuilder st = new StringBuilder();
    for (int i = 0; i < array.length; i++) {
        word = array[i];
        if (word.contains(word_search)) {
            st.append("<b><i>" + word.trim() + "</i></b>");
            total++;
        } else {
            st.append(word);
        }
        st.append("<br>");
    }
    textView.setText(Html.fromHtml("" + st));
    text_total.setText("Ergebnisse: " + total);
}

Now, the problem is that when I search something, I must pay attention to the higher lower case. And the question is: How can I make the higher lower case equal, so that I can search something without paying attention to this?

Thanks!

android
  • 29
  • 10

1 Answers1

1

Simply use toLowerCase() to make contains case sensitive

  word = array[i].toLowerCase();
     if (word.contains(word_search.toLowerCase())) {

To append the original use

    int total = 0;
    String word_search = Etxt.getText().toString().trim().toLowerCase();
    //                                                     ^^^ 
    String fullTxt = textView.getText().toString();
    String[] array = fullTxt.split("\n");
    String word;
    StringBuilder st = new StringBuilder();
    for (int i = 0; i < array.length; i++) {
        word = array[i];
        if (word.toLowerCase().contains(word_search)) {
            //   ^^^^ won't change the original case , to append later 
            // toLowerCase() returns the new string object in lower case
            // so original "word" string object stays the same 
            st.append("<b><i>" + word.trim() + "</i></b>");
            total++;
        } else {
            st.append(word);
        }
        st.append("<br>");
    }
    textView.setText(Html.fromHtml("" + st));
    text_total.setText("Ergebnisse: " + total);
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • @android try the updated answer , i had that in mind so see the example code – Pavneet_Singh Jan 22 '17 at 11:23
  • @android thanks for compliment , i am glad that i could help , happy coding – Pavneet_Singh Jan 22 '17 at 11:27
  • I have another question. It's almost the same I think. But how can I make this with accented letters? Thanks for answer! :) – android Jan 22 '17 at 16:30
  • @android It suppose to have no issue , `À` will be converted to `à` using `toLowerCase` function – Pavneet_Singh Jan 22 '17 at 16:45
  • I know. I asked the question wrong. I mean that À will be converted to A or a. Can you help me please? Thanks! – android Jan 22 '17 at 17:25
  • @android that would be wrong , `À` will be converted to `à` not to `A` or `a` but if you want to convert accented chars to english alphabets then you have to do it manually , you can use [this post](http://stackoverflow.com/questions/3322152/is-there-a-way-to-get-rid-of-accents-and-convert-a-whole-string-to-regular-lette) – Pavneet_Singh Jan 22 '17 at 17:31