Find out if a String contain a value in an array in Java -


Is there a way to find out if there is any value in your string string []?

This is my code:

  string s = "hello world! My name is bao" ;; String [] arr = new string [] {"o", "!", "-", "y", "z"}; (String item: arr) {if (s.contains (item)) {System.out.println ("string contains:" + item); } Else {System.out.println ("Not included in the string:" + item); }}   

Is there a way to do this? I do not want to use the for loop for this.

It may slow down when the array contains 4000+ strings.

For large arrays of strings, first it is to assign an array and target string to a HashSet < / code>. Duplicate characters will be removed from being a set , and compared with having the hash version will be faster. After that you can do some work of quick set subtraction to get your answer:

  string s = "hello world! My name is bao" ;; String [] arr = new string [] {"o", "!", "-", "y", "z"}; Set up & lt; String & gt; SSet = new hashset & lt; String & gt; (Arrays.asList (s.split ("(?! ^ ^ ^)"))); Set up & lt; String & gt; ArrSet = new hashset & lt; String & gt; (Arrays.asList (arr)); Collections & lt; String & gt; NotFound = Archive UTree SHOWUP (ARRAT, ASSET); Collections & lt; String & gt; Found = Archive UTRI. Shutatric (ARRset, noFand);    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -