2

in my android application i have tried to compare string with arraylist check it out my below code

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

Can anybody help ??

2
  • Why don't you just .split(","); Commented Jul 21, 2015 at 8:25
  • Not possible with this scenario. Because you can not get San Francisco when split the name. Commented Jul 21, 2015 at 8:52

3 Answers 3

2

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

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).

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks a lot it helps
i am facing one more issue, when i use this code it returns all string which are all match with city ex: my arral list having both san fransico and san then this code return both san and san fransisco, where us i need only san fransisco
You can use break; and continue; to control the for loop? Like add break; to the if-statement when you want to stop at first match?
No i think it wont work because what's the solution if my required string is in end of the array, even i am not sure about my first match is the one i want, i need exact match, any idea
I see now, can you add if-s for checking that the string is exactly the one you want?
|
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]);
        }
    }

1 Comment

No it gives me separated output
1

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

change

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

to

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.