字符串比较

时间:2013-01-22 17:34:46

标签: java string

我想比较两个字符串,如果第一个字符串中存在第二个字符串,则返回true。

   @Test
   public bool compareStrings{
        String text1="hello world";
        String text2="hello but some xyz world and xzy bla bla";
        /* I want to check if text2 contains text1 */
   }

3 个答案:

答案 0 :(得分:2)

尝试这样的事情: -

text2.toLowerCase().contains(text1.toLowerCase())

答案 1 :(得分:1)

你想这样做:

if (text2.indexOf(text1) > -1)
{
  // text2 contains text1
}

答案 2 :(得分:0)

返回布尔值

return text2.contains(text1);