Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The most efficient way to search for an array of strings in another string

I have a large arrray of strings that looks something like this: String temp[] = new String[200000].

I have another String, let's call it bigtext. What I need to do is iterate through each entry of temp, checking to see if that entry is found in bigtext and then do some work based on it. So, the skeletal code looks something like this:

for (int x = 0; x < temp.length; x++) {
  if (bigtext.indexOf(temp[x]) > -1 {

  //do some stuff
  } else continue;
}

Because there are so many entries in temp and there are many instances of bigtext as well, I want to do this in the most efficient way. I am wondering if what I've outlined is the most efficient way to iterate through this search of if there are better ways to do this.

Thanks,

Elliott

like image 912
Elliott Avatar asked Mar 06 '12 13:03

Elliott


People also ask

How do you compare two strings in an array?

equals() Method. Java Arrays class provides the equals() method to compare two arrays. It iterates over each value of an array and compares the elements using the equals() method.

Is there a substring for arrays?

To extract a substring as an array of characters in Java, use the getChars() method. Let's say the following is our string and character array. String str = "World is not enough!"; char[] chArr = new char[10]; Now, use the getChars() method to extract a substring.

Can we compare array to string in Java?

The java. util. Arrays class provides two convenient methods for array comparison – equals() and deepEquals() . We can use either method for string array comparison.


1 Answers

I think you're looking for an algorithm like Rabin-Karp or Aho–Corasick which are designed to search in parallel for a large number of sub-strings in a text.

like image 170
Dave R Avatar answered Oct 10 '22 00:10

Dave R