【问题标题】:Contains method case-insensitive without major code changes?包含方法不区分大小写而没有重大代码更改?
【发布时间】:2014-12-21 23:36:06
【问题描述】:

有没有办法忽略 contains() 方法的大小写,同时让代码 sn-p 大致相同?

/**
 * 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;
}

这是整个 SpellChecker 类:

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

DicReader 类只需要一个给定的文本文件并从中“制作”一个字典。以防万一我把它贴出来:

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

所以问题是我需要检查天气或给定的单词是否包含我称之为“文本”的文本 sn-p/子字符串,但是字符串的 contains 方法区分大小写。我做了一些研究,并注意到要删除区分大小写,您必须导入特定的库,并且新 contains 方法的语法不同,它与我目前拥有的代码不能很好地配合。然后我想知道是否有办法使 contains() 不区分大小写但保留代码的结构。

【问题讨论】:

标签: java string arraylist contains case-insensitive


【解决方案1】:

将两个字符串都设为小写(或大写)

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

【讨论】:

  • 谢谢!我实际上最终使用了这个精确的小写解决方案。
猜你喜欢
  • 2012-02-18
  • 2013-01-16
  • 2014-08-04
  • 2023-04-02
  • 2019-08-08
  • 2012-02-03
  • 2011-10-20
  • 2014-06-10
相关资源
最近更新 更多