在Android中使用Arraylist搜索字符串

在我的Android应用程序中,我试图比较字符串与arraylist检查出我的下面的代码

ArrayList<String> namelist = new ArrayList<String>();
        ArrayList<String> withoutcomma = new ArrayList<String>();

        namelist.add("chennai , In");
        namelist.add("mumbai, In");
        namelist.add("Usak,TR");
        namelist.add("Ushuaia,AR");
        namelist.add("San Francisco,US");

        for (int i = 0; i < namelist.size(); i++) {
            String[] value = namelist.get(i).split("\\s*,\\s*");
            withoutcomma.add(value[0]);
            withoutcomma.add(value[1]);
        }

        String name = "i want to go chennai to mumbai";
        String[] splitname = name.split(" ");

        for (int i = 0; i < splitname.length; i++) {
            if (withoutcomma.contains(splitname[i])) {
                System.out.println(splitname[i]);
            }
        }

it returns correct output

它返回正确的输出

chennai 
mumbai

but not able to get the value for san fransisco because white space, i am totally struck, even i have used Pattern and matches but not able to handle the white space

但是无法获得san fransisco的价值,因为白色空间,我完全被击中,即使我已经使用了模式和匹配但不能处理白色空间

3 个解决方案

#1


2  

Instead of splitting by white space, you could look for values of your withoutcomma array in the String name.

您可以在String名称中查找withoutcomma数组的值,而不是按空格分割。

for (String str : withoutcomma)
{
    if (name.contains(str))
    {
        System.out.println("Found city: " + str + " at position: " + name.indexOf(str));
    }
}

And if the order of the cities matters, you can save the index of the value that was found in String name with int idx = name.indexOf(str).

如果城市的顺序很重要,您可以使用int idx = name.indexOf(str)保存在String name中找到的值的索引。

#2


2  

Split the words at commas as well as at any spaces. So, when ever you get a multiple word place name, store it in the Array, and then use it to compare. works for me.

用逗号和任何空格分隔单词。因此,当您获得多字地名时,将其存储在数组中,然后使用它进行比较。适合我。

Use this :

用这个 :

    ArrayList<String> namelist = new ArrayList<String>();
    ArrayList<String> withoutcomma = new ArrayList<String>();

    namelist.add("chennai , In");
    namelist.add("mumbai, In");
    namelist.add("Usak,TR");
    namelist.add("Ushuaia,AR");
    namelist.add("San Francisco,US");

    for (int i = 0; i < namelist.size(); i++) {
        String[] value = namelist.get(i).split("[(\\s*,\\s*)\\s]");
        for(String word : value) {
            withoutcomma.add(word);
        }
    }

    String name = "i want to go San Francisco to mumbai";
    String[] splitname = name.split(" ");

    for (int i = 0; i < splitname.length; i++) {
        if (withoutcomma.contains(splitname[i])) {
            System.out.println(splitname[i]);
        }
    }

#3


1  

you can use trim() to omit space in San Francisco,

你可以使用trim()来省略旧金山的空间,

change

更改

 String[] value = namelist.get(i).split("\\s*,\\s*");

to

 String[] value = namelist.get(i).trim().split("\\s*,\\s*");
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

apple_51426592

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值