2

So, I want to compare from string to array of strings

String [] string = new String [10];
string [1] = "pencil";
string [2] = "pen";
string [3] = "eraser";

how do i compare string to the array of strings from above?

4
  • Do you mean "Compare a String[] with multiple String" ? Commented Nov 5, 2014 at 10:18
  • Your question is unclear and very generic to answer, if you elaborate I might be able to help you. Commented Nov 5, 2014 at 10:20
  • how can it be single and multiple string? Commented Nov 5, 2014 at 10:24
  • lol, i mean array. i didnt know the name of String [], isn't the name of variable is array? Commented Nov 5, 2014 at 10:28

3 Answers 3

2

A quicker way is to use this code

if(Arrays.asList(string).contains(search_string))
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, it do as i wish, need to edit a few more code.
1

If I understand you correctly, Write a loop, where iterate multiple string(example array) and equals with src string.

String [] string = new String [10];
...
String src = ..;//src string

for(String string : string){
    if(src.equals(string)){
       //Equal
    }
}

4 Comments

What is the secret plan ?
when i'm comparing string, it should be comparing from all multiple list, not the specific one.
Adrian Totolici's answer will cover that but it is actually the same result. it's just that, with this code, you have more control about what string is actually equal and not only "the array contains an equal string"
any reference javadoc that i could use?
0

Try using this:

for (int i = 0; i < string.length; i++) {
    if (ch.equals(string[i])) { // ch is the string to compare
    System.out.println("equal");//or whatever your action is
    }
}

EDIT:

If what you are seeking to do is to verify if the searched string appears in the string[i] you can try:

string[0] = "pentagon";
string[1] = "pencil";
string[2] = "pen";
string[3] = "eraser";
string[4] = "penny";
string[5] = "pen";
string[6] = "penguin";
string[7] = "charp";
string[8] = "charpen";
string[9] = "";
String ch = "pen";

Then test if the arrays elements contain the searched string:

for (int i = 0; i < string.length; i++) {
    if (string[i].indexOf(ch) != -1) {
    System.out.println(string[i]+" contains "+ch);
    } else
    System.out.println(string[i]+" doesn't contain "+ch);
}

EDIT 2:

int i=0;
Boolean found=false;
while(i < string.length && !found) {
    if (string[i].indexOf(ch) != -1) {
    found=true;
    }
 i++;
}
if(found){
system.out.println(ch+" is found");
}

5 Comments

Unfortunately the code didn't work out. Say if i input "charp" to the variable ch, it display string[i] doesn't contain charp.
No the result then is: ***pentagon doesn't contain charp ***pencil doesn't contain charp ***pen doesn't contain charp ***eraser doesn't contain charp ***penny doesn't contain charp ***pen doesn't contain charp ***penguin doesn't contain charp ***charp contains charp ***charpen contains charp *** doesn't contain charp
hmm, i see. Basically i want the code to check all the list whether it have ch and then output to the screen if ch on the list.
Then Adrian answer is the more appropriate.
thanks anyway for the time you spare for me. The adrian's code is simple, yet it do a job lot faster.

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.