Contains method case-insensitive without major cod

2019-03-05 09:18发布

Is there a way to ignore the case for the contains() method but at the same time leave the code snippet more or less the same?

/**
 * This method returns a list of all words from the dictionary that include the given substring.
 *
 */
public ArrayList<String> wordsContaining(String text)
{
    int index = 0;
    ArrayList<String> wordsContaining = new ArrayList<String>();      
    while(index<words.size())
    {
        if((words.get(index).contains(text)))
        {
            wordsContaining.add(words.get(index));
        }
        index++;
    }
    return wordsContaining;
}

here is the whole SpellChecker Class:

public class SpellChecker
{
     private ArrayList<String> words;
     private DictReader reader;

    /**
     * Constructor for objects of class SpellChecker
     */

public SpellChecker()
{
    reader = new DictReader("words.txt");
    words = reader.getDictionary();
}

/**
 * This method returns the number of words in the dictionary.
 * 
 */
public int numberOfWords()
{
    return words.size();
}

/**
 * This method returns true, if (and only if) the given word is found in the dictionary.
 *
 */
public boolean isKnownWord(String word)
{
    int index =0;
    while(index < words.size())
    {
        if(words.get(index).equals(word))
        {
            return true;
        }
        index++;
    }
    return false;
}

/**
 * This method returns true if (and only if) all words in the given wordList are found in the dictionary.
 */
public boolean allKnown(ArrayList<String> wordList)
{
   for(String word : wordList)
   {
       if(isKnownWord(word))
       {
           return true;
        }

   }
    return false;
}

/**
 * This method tests the allKnown method.
 */
public boolean testAllKnown()
{
    ArrayList<String> testWords = new ArrayList<String>();
    testWords.add("Abu");
    testWords.add("Chou");
    if(allKnown(testWords))
    {
        return true;
    }
    else
    {
        return false;
    }

}

/**
 * This method returns a list of all words from the dictionary that start with the given prefix.
 *
 */
public ArrayList<String> wordsStartingWith(String prefix)
{
    int index = 0;
    ArrayList<String> wordsStartingWith = new ArrayList<String>();
    while(index<words.size())
    {
        if(words.get(index).startsWith(prefix))
        {
            wordsStartingWith.add(words.get(index));

        }
        index++;
    }
    return wordsStartingWith;
}    

/**
 * This method returns a list of all words from the dictionary that include the given substring.
 *
 */
public ArrayList<String> wordsContaining(String text)
{
    int index = 0;
    ArrayList<String> wordsContaining = new ArrayList<String>();      
    while(index<words.size())
    {
        if((words.get(index).contains(text)))
        {
            wordsContaining.add(words.get(index));
        }
        index++;
    }
    return wordsContaining;
}

The DicReader class simply takes a give text file and "Makes" a dictionary out of it. I'll put it up just in case:

public class DictReader
{
  private ArrayList<String> dict;
  private String filename;

  /**
   * Create a DictReader instance from a file.
   */

public DictReader(String filename)
{
    loadDictionary(filename);
    this.filename = filename;
}

/**
 * Return the dictionary as a list of words.
 */
public ArrayList<String> getDictionary()
{
    return dict;
}

/**
 * Accept a new dictionary and save it under the same name. Use the new
 * one as our dictionary from now on.
 */
public void save(ArrayList<String> dictionary)
{
    try {
        FileWriter out = new FileWriter(filename);
        for(String word : dictionary) {
            out.write(word);
            out.write("\n");
        }
        out.close();
        dict = dictionary;
    }
    catch(IOException exc) {
        System.out.println("Error writing dictionary file: " + exc);
    }
}

/**
 * Load the dictionary from disk and store it in the 'dict' field.
 */
private void loadDictionary(String filename)
{
    dict = new ArrayList<String>();

    try {
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String word = in.readLine();
        while(word != null) {
            dict.add(word);
            word = in.readLine();
        }
        in.close();
    }
    catch(IOException exc) {
        System.out.println("Error reading dictionary file: " + exc);
    }
}
}

So the question is I need to check weather or not a given word contains a text snippet/substring which I have called "text" however the contains method for strings is case sensitive. I have done some research and noticed that to remove the case sensitivity you must import a specific library and the syntax for the new contains method is different and it does not play well with what I currently have as code. I was then wondering if there is a way to make contains() case-insensitive but preserve the structure of the code.

1条回答
2楼-- · 2019-03-05 09:53

Make both strings lowercase(or uppercase)

public ArrayList<String> wordsContaining(String text)
{
    int index = 0; 
    text = text.toLowerCase();
    ArrayList<String> wordsContaining = new ArrayList<String>();      
    while(index<words.size())
    {
        if((words.get(index).toLowerCase().contains(text)))
        {
            wordsContaining.add(words.get(index));
        }
        index++;
    }
    return wordsContaining;
}
查看更多
登录 后发表回答