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.
-
1Guys why the down votes. Is there something wrong. Please do let me know also.KISHORE_ZE– KISHORE_ZE2015年08月28日 18:44:24 +00:00Commented Aug 28, 2015 at 18:44
-
Start with docs.oracle.com/javase/7/docs/api/java/lang/…bcsb1001– bcsb10012015年08月28日 18:45:05 +00:00Commented Aug 28, 2015 at 18:45
-
Is the array of one word strings only, or can the array entries have full sentences as well?folkol– folkol2015年08月28日 18:59:03 +00:00Commented Aug 28, 2015 at 18:59
-
Array one or 2 words. And String sentencesKISHORE_ZE– KISHORE_ZE2015年08月28日 18:59:53 +00:00Commented 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?folkol– folkol2015年08月28日 19:01:25 +00:00Commented Aug 28, 2015 at 19:01
7 Answers 7
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 ");
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.
8 Comments
textInput
is that what you wanted? added a demo alsoyou 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])
4 Comments
txArray = textInput.split(" ");
creates array of wordstxArray = "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++;
}
1 Comment
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);
}
}
1 Comment
Try this;
Sting text2check = "Your Name":
for(int t = 0; t < array.length; t++)
{
if (text2check.equals(array[t])
// Process it Here
break;
}
5 Comments
.equals
should probably be .contains
"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;
}
5 Comments
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
}
}
5 Comments
stringArray[i].contains(inputText)
should probably be inputText.contains(stringArray[i])