Java String finder - How to switch case sensitivity?

I have a method that looks up the file for the lines you give it and returns a counter. However, I am having problems with case sensitivity. Here's the way:

public int[] count(String[] searchFor, String fileName) {
    int[] counts = new int[searchFor.length];
    try {
        FileInputStream fstream = new FileInputStream(fileName);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null) {
        for (int i = 0; i < searchFor.length; i++) {
            if (strLine.contains(searchFor[i])) {
                counts[i]++;
            }
        }
    }
    in.close();
    } catch (Exception e) {// Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
    return counts;
}

      

I am parsing an array of arrays of strings to look for in a file. However, some strings in the array need to be searched to ignore the case. How can I change my method to accommodate this since I am completely stumped.

This method is used by multiple classes, so I can't just insert an if statement into the for loop that says

if(i == 4) ... 
... strLine.toLowerCase().contains(searchFor[i].toLowerCase()) ...

      

Any ideas on how I can better implement this functionality?

Thank you Jordan

+3


source to share


4 answers


Since you have an array of strings with entries that need to be handled differently (for example, case sensitive and case insensitive), I recommend creating your own custom search class case

:

public class SearchTerm {
    private final String term;
    private final boolean caseSensitive;

    public SearchTerm(final String term, final boolean caseSensitive) {
        this.term = term;
        this.caseSensitive = caseSensitive;
    }

    public String getTerm() {
        return term;
    }

    public boolean isCaseSensitive() {
        return caseSensitive;
    }
}

      

Then you can use this class to replace the current array:

count(SearchTerm[] searchFor, String fileName)

      



And use it in your search method:

for (int i = 0; i < searchFor.length; i++) {
    if (searchFor[i].isCaseSensitive()) {
        if (strLine.contains(searchFor[i].getTerm())) {
            counts[i]++;
        }
    }
    else {
        // this line was "borrowed" from Maroun Marouns answer (you can also use different methods to search case insensitive)
        if (Pattern.compile(strLine, Pattern.CASE_INSENSITIVE).matcher(searchFor[i].getTerm()).find()) { 
            counts[i]++;
        }
    }
}

      

This way you avoid "global" case-sensitive or case-insensitive searches, and you can treat each search term differently.

+2


source


Why not just add boolean ignoreCase

to the method parameters?

Or you can make an overloaded method.



public int[] count(String[] searchFor, String fileName, boolean ignoreCase) {}

      

+4


source


You can use Pattern#CASE_INSENSITIVE

and implement your own method:

private boolean myContains(your_parameters, boolean caseSensitive) {
    if(!caseSensitive)
        return Pattern.compile(strLine, Pattern.CASE_INSENSITIVE).matcher(searchFor[i]).find();
    return strLine.contains(searchFor[i]);
}

      

+1


source


Apache StringUtils.ContainsIgnoreCase()

to the rescue. More on this here .

0


source







All Articles