如何在整个字符串中搜索特定单词? [英] How to search the whole string for a specific word?

查看:56
本文介绍了如何在整个字符串中搜索特定单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,它搜索字符串数组,如果输入的字符串与字符串的第一个字符匹配,则返回结果:

I have this code which searches a string array and returns the result if the input string matches the 1st characters of a string:

for (int i = 0; i < countryCode.length; i++) {
            if (textlength <= countryCode[i].length()) {
                if (etsearch
                        .getText()
                        .toString()
                        .equalsIgnoreCase(
                                (String) countryCode[i].subSequence(0,
                                        textlength))) {
                    text_sort.add(countryCode[i]);
                    image_sort.add(flag[i]);
                    condition_sort.add(condition[i]);
                }
            }
        }

但是我也想获得那些字符串,其中输入字符串不仅在第一个字符中匹配,而且在字符串中的任何地方都匹配?该怎么做?

But i want to get those string also where the input string matches not only in the first characters but also any where in the string? How to do this?

推荐答案

您可以通过三种方式搜索字符串是否包含子字符串:

You have three way to search if an string contain substring or not:

String string = "Test, I am Adam";
// Anywhere in string
b = string.indexOf("I am") > 0;         // true if contains 

// Anywhere in string
b = string.matches("(?i).*i am.*");     // true if contains but ignore case

// Anywhere in string
b = string.contains("AA")  ;             // true if contains but ignore case

这篇关于如何在整个字符串中搜索特定单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆