java 如何检查数组是否包含字符串中的特定单词并获取它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32277771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if an Array contains a particular word in a String and get it?
提问by KISHORE_ZE
I have a String[]
and an input String
:
我有一个String[]
和一个输入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.
我想要做的是检查 中是否String
包含一个单词Array
,就像这样。
Ex: textInput = "for example"
and ArrayEx[0] = "example"
例如:textInput = "for example"
和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 wordpresent in the Array. Even if it is from an ArrayList
I have no problem.
但它检查完整String
吗?如何检查Array 中是否String
包含特定单词。即使它来自一个ArrayList
我也没有问题。
Also if yes, can I get that word from the String[]
? i.e., in the above case get the String
"example".
另外,如果是,我可以从String[]
? 即,在上述情况下获取String
“示例”。
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
说v2txt
是"awesome airport"
检测到的日志永远不会出现,即使所有其他记录表明它工作
Edit2:
编辑2:
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.
我很尴尬,我犯了这样一个愚蠢的错误。我的数组被错误地声明。"
每个之前都应该有一个,
。我真是个大笨蛋!抱歉,将更改它并让您知道。
回答by silentprogrammer
First of all it has nothing to do with android
首先它与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.
等等与您的数组声明的其他值,它有一个索引。
回答by Want2bExpert
Try this;
试试这个;
Sting text2check = "Your Name":
Sting text2check = "你的名字":
for(int t = 0; t < array.length; t++)
{
if (text2check.equals(array[t])
// Process it Here
break;
}
回答by Turtle
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 数组,并搜索所有字符串以查找在字符串中找到的特定字符序列。此外,原生 Android 应用程序是用 Java 语言编写的。您可能会发现阅读更多有关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
}
}
回答by 13th Ghost
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++;
}
回答by Bacteria
You can use Java - Regular Expressions.
您可以使用 Java - 正则表达式。
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);
}
}
回答by folkol
"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
爪哇 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,则必须自己重新实现“anyMatch”和“过滤器”:
Java 7
爪哇 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;
}
回答by krzydyn
you can split your string and get array of words
您可以拆分字符串并获取单词数组
txArray = textInput.split(" ");
then for each element in txArray check if
然后对于 txArray 中的每个元素检查是否
Arrays.asList(ArrayEx).contains(txArray[i])