1

I have a String[] and an input String:

String[] ArrayEx = new String[1];
String textInput = "a whole bunch of words"

What I want to do is check if the String contains a word present in the Array, like this.

Ex: textInput = "for example" and ArrayEx[0] = "example"

I know about this method:

Arrays.asList(yourArray).contains(yourValue)

but it checks the full String right? How do I check if the String contains a particular word present in the Array. Even if it is from an ArrayList I have no problem.

Also if yes, can I get that word from the String[]? i.e., in the above case get the String "example".

EDIT:

public void searchNearestPlace(String v2txt)
{
 Log.e("TAG", "Started");
 v2txt = v2txt.toLowerCase();
 String[] places = {"accounting, airport, amusement_park, aquarium, art_gallery, atm, bakery, bank, bar, beauty_salon, bicycle_store, book_store, bowling_alley, bus_station, cafe, campground, car_dealer, car_rental, car_repair, car_wash, casino, cemetery, church, city_hall, clothing_store, convenience_store, courthouse, dentist, department_store, doctor, electrician, electronics_store, embassy, establishment, finance, fire_station, florist, food, funeral_home, furniture_store, gas_station, general_contractor, grocery_or_supermarket, gym, hair_care, hardware_store, health, hindu_temple, home_goods_store, hospital, insurance_agency, jewelry_store, laundry, lawyer, library, liquor_store, local_government_office, locksmith, lodging, meal_delivery, meal_takeaway, mosque, movie_rental, movie_theater, moving_company, museum, night_club, painter, park, parking, pet_store, pharmacy, physiotherapist, place_of_worship, plumber, police, post_office, real_estate_agency, restaurant, roofing_contractor, rv_park, school, shoe_store, shopping_mall, spa, stadium, storage, store, subway_station, synagogue, taxi_stand, train_station, travel_agency, university, veterinary_care, zoo"};
 int index;
 for(int i = 0; i<= places.length - 1; i++)
 {
 Log.e("TAG","for");
 if(v2txt.contains(places[i]))
 {
 Log.e("TAG", "sensed?!");
 index = i;
 }
}

Say v2txt was "awesome airport" the sensed Log never does appear even though all other logs indicate it working

Edit2:

I am so embarrassed that I made such a dunder head mistake. My array is declared wrongly. There should be a " before every ,. I am such a big idiot! Sorry will change it and let you know.

asked Aug 28, 2015 at 18:40
8
  • 1
    Guys why the down votes. Is there something wrong. Please do let me know also. Commented Aug 28, 2015 at 18:44
  • Start with docs.oracle.com/javase/7/docs/api/java/lang/… Commented Aug 28, 2015 at 18:45
  • Is the array of one word strings only, or can the array entries have full sentences as well? Commented Aug 28, 2015 at 18:59
  • Array one or 2 words. And String sentences Commented Aug 28, 2015 at 18:59
  • And you want to know if the sentence contains any of the words from the array, or just the (1 or 2 word) phrases from the array? Commented Aug 28, 2015 at 19:01

7 Answers 7

2

First of all it has nothing to do with android

Second the solution

boolean flag = false;
String textInput = "for example";
int index = 0;
String[] yourArray = {"ak", "example"};
for (int i = 0; i <= yourArray.length - 1; i++) {
 if (textInput.contains(yourArray[i])) {
 flag = true;
 index = i;
 }
}
if (flag) 
 System.out.println("found at index " + index);
else 
 System.out.println("not found ");

DEMO

EDIT :

Change your array to

String[] places = {"accounting", "airport", "amusement_park" };

and so on with other values with your array declaration it has one index.

answered Aug 28, 2015 at 18:46

8 Comments

But doesn't this check the full String?
And 2 sorry this was for an android project but you are right this code does not relate to android. I'll remove it.
Thanks for the edit but still won't it check the full String instead of one word by one.
@KISHORE_ZE this code take the string at each index of array and finds if it is a substring of textInput is that what you wanted? added a demo also
It worked. I am sorry all of them were correct but since I could give only one correct I gave it to the guy who first answered. Sorry. But I gave everyone one up vote.
|
1

you can split your string and get array of words

txArray = textInput.split(" ");

then for each element in txArray check if

Arrays.asList(ArrayEx).contains(txArray[i])
answered Aug 28, 2015 at 18:46

4 Comments

How would I get them into an array. And how would I get the word in the array.
txArray = textInput.split(" "); creates array of words
It works but it's kinda complex. Like I int i will be the new arrays lenght or it gives a npe. But
I am sorry all of them were correct but since I could give only one correct I gave it to the guy who first answered. Sorry. But I gave everyone one up vote.
1
txArray = "Hello I'm your String";
String[] splitStr = txArray.split(" ");
int i=0;
while(splitStr[i]){
if(Arrays.asList(ArrayEx).contains(txArray[i])){
System.out.println("FOUND");
}
i++;
}
answered Aug 28, 2015 at 18:59

1 Comment

I am sorry all of them were correct but since I could give only one correct I gave it to the guy who first answered. Sorry. But I gave everyone one up vote and BTW it's a duplicate. Still no problem 😉
1

You can use Java - Regular Expressions.

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Testing {
 public static void main(String[] args) {
 String textInput = "for example";
 String[] arrayEx = new String[1];
 arrayEx[0] = "example";
 Pattern p = Pattern.compile(arrayEx[0]);
 Matcher m = p.matcher(textInput);
 boolean matchedFoundStatus = false;
 while (m.find()) {
 matchedFoundStatus = true;
 }
 System.out.println("matchedFoundStatus:" + matchedFoundStatus);
 }
}
answered Aug 28, 2015 at 19:00

1 Comment

Thanks for the answer. I am sorry all of them were correct but since I could give only one correct I gave it to the guy who first answered. Sorry. But I gave everyone one up vote.
1

Try this;

Sting text2check = "Your Name":

for(int t = 0; t < array.length; t++)
{
if (text2check.equals(array[t])
// Process it Here
break;
}
answered Aug 28, 2015 at 18:52

5 Comments

There is no to String available on an array
I will edit my answer, i was trying to say the return value of array is string because you to store string in it. Alternatively use String.valueof(array[t]) if it is not String already.
Ok understood. No need of to String and it's working now.
I am sorry all of them were correct but since I could give only one correct I gave it to the guy who first answered. Sorry. But I gave everyone one up vote.
Also for my purpose .equals should probably be .contains
1

"How do I check if the String contains a particular word present in the Array?" is the same thing as Is there an element in the array, for which the input string contains this element

Java 8

String[] words = { "example", "hello world" };
String input = "a whole bunch of words";
Arrays.stream(words).anyMatch(input::contains);

(The matching words can also be extracted, if needed:)

Arrays.stream(words)
 .filter(input::contains)
 .toArray();

If you are stuck with Java 7, you will have to re-implement "anyMatch" and "filter" yourself:

Java 7

boolean anyMatch(String[] words, String input) {
 for(String s : words)
 if(input.contains(s))
 return true;
 return false;
}
List<String> filter(String[] words, String input) {
 List<String> matches = new ArrayList<>();
 for(String s : words)
 if(input.contains(s))
 matches.add(s);
 return matches;
}
answered Aug 28, 2015 at 19:43

5 Comments

Thanks. But it's answered now. I am sorry all of them were correct but since I could give only one correct I gave it to the guy who first answered. Sorry. But I gave everyone one up vote.
Don't be sorry :) Just select the answer that helped you, and good luck with your program!
Ok thanks. Wish I could mark multiple answers as correct though.
All upvoted answers will be "correct" in some sense, the "selected" answer is just so that people with the same problem as you can find "any solution" at a glance.
Ya but still for you guys 15 points out right. I feel bad I can't give you them to you guys even after all the effort you guys put into it.
1

This will take an String array, and search through all the strings looking for a specific char sequence found in a string. Also, native Android apps are programmed in the Java language. You might find it beneficial to read up more on Strings.

String [] stringArray = new String[5];
//populate your array
String inputText = "abc";
for(int i = 0; i < stringArray.length; i++){
 if(inputText.contains(stringArray[i]){
 //Do something
 }
}
answered Aug 28, 2015 at 18:53

5 Comments

I am sorry all of them were correct but since I could give only one correct I gave it to the guy who first answered. Sorry. But I gave everyone one up vote.
Also for my purpose stringArray[i].contains(inputText) should probably be inputText.contains(stringArray[i])
Yes, I see that upon re reading the question. I thought you wanted the answer the other way around. My mistake
Hey no problem. It was almost correct want it and also you took the effort to help others right? No problem
Yes it does. Thank you

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.