Contains method case-insensitive without major code changes?

后端 未结 1 596
误落风尘
误落风尘 2021-01-27 22:30

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          


        
相关标签:
1条回答
  • 2021-01-27 23:09

    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;
    }
    
    0 讨论(0)
提交回复
热议问题