ColonelSpuds
ColonelSpuds

Reputation: 81

Java String finder - How can I toggle case sensitivity?

I have a method that searches a file for the strings you give it and returns a count. However I am having trouble with case sensitivity. Here is the method:

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 parse it an array of Strings to search for in the file. However some of the Strings in the array need to be searched for ignoring case. How can I change my method to accomodate this as am completely stumped.

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

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

Any ideas of how I can better implement this functionality?

Thanks, Jordan

Upvotes: 3

Views: 295

Answers (4)

Tom
Tom

Reputation: 17567

Since you have an array of Strings with entries that need to be treated differently (i.e. case sensitive and case insensitive), I recommend to create an own class for search terms with a case setting:

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 that 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]++;
        }
    }
}

That way you avoid a "global" case sensitive or case insensitive search and you can treat each search term differently.

Upvotes: 2

Maroun
Maroun

Reputation: 95948

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]);
}

Upvotes: 1

Darshan Mehta
Darshan Mehta

Reputation: 30809

Apache StringUtils.ContainsIgnoreCase() to the rescue. More on it here.

Upvotes: 0

austin wernli
austin wernli

Reputation: 1801

Why not just add a boolean ignoreCase in the method params?

Or you could make an overloaded method.

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

Upvotes: 4

Related Questions