【问题标题】:How to check if an Array contains a particular word in a String and get it?如何检查数组是否包含字符串中的特定单词并获取它?
【发布时间】:2015-08-28 18:40:51
【问题描述】:

我有一个String[] 和一个输入String

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

我要做的是检查String 是否包含Array 中存在的单词,像这样。

例如:textInput = "for example"ArrayEx[0] = "example"

我知道这种方法:

Arrays.asList(yourArray).contains(yourValue)

但它会检查完整的String 对吗?如何检查 String 是否包含数组中存在的特定 word。即使是来自ArrayList 我也没有问题。

如果是的话,我可以从String[] 得到那个词吗?即,在上述情况下,获取String“示例”。

编辑:

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;
    }
}

假设v2txt"awesome airport",即使所有其他日志都表明它正常工作,感知的日志也不会出现

编辑2:

我很尴尬,我犯了这样一个愚蠢的错误。我的数组被错误地声明了。每个, 之前应该有一个"。我真是个大白痴! 很抱歉会更改并通知您。

【问题讨论】:

  • 各位为什么投反对票。有什么不对。也请告诉我。
  • 是只有一个单词字符串的数组,还是数组条目也可以有完整的句子?
  • 排列一到两个单词。和字符串句子
  • 您想知道句子是否包含数组中的任何单词,还是仅包含数组中的(1 或 2 个单词)短语?

标签: java arrays string arraylist


【解决方案1】:

首先它与android无关

第二个解决方案

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

编辑

将您的数组更改为

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

在你的数组声明中使用其他值,它有一个索引。

【讨论】:

  • 但这不是检查完整的字符串吗?
  • 和 2 对不起,这是一个 android 项目,但你是对的,这段代码与 android 无关。我会删除它。
  • 感谢您的编辑,但它仍然不会检查完整的字符串而不是一个字一个字。
  • @KISHORE_ZE 此代码在数组的每个索引处获取字符串,并查找它是否是textInput 的子字符串,这是您想要的吗?还添加了一个演示
  • 成功了。很抱歉他们都是正确的,但因为我只能给出一个正确的答案,所以我把它给了第一个回答的人。对不起。但我给了每个人一票。
【解决方案2】:

您可以拆分字符串并获取单词数组

txArray = textInput.split(" ");

然后为 txArray 中的每个元素检查是否

Arrays.asList(ArrayEx).contains(txArray[i])

【讨论】:

  • 如何将它们放入数组中。以及如何在数组中获取单词。
  • txArray = textInput.split(" "); 创建单词数组
  • 它有效,但有点复杂。就像我 int 我将是新的数组长度,否则它会给出一个 npe。但是
  • 我很抱歉他们都是正确的,但由于我只能给出一个正确的答案,所以我把它给了第一个回答的人。对不起。但我给了每个人一票。
【解决方案3】:
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++;
}

【讨论】:

  • 我很抱歉他们都是正确的,但由于我只能给出一个正确的答案,所以我把它给了第一个回答的人。对不起。但我给了每个人一票,顺便说一句,这是重复的。还是没问题?
【解决方案4】:

您可以使用 Java - 正则表达式。

正则表达式是一个特殊的字符序列,可以帮助您匹配或查找其他字符串或字符串集,使用模式中的特殊语法。它们可用于搜索、编辑或操作文本和数据。

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);
    }

}

【讨论】:

  • 感谢您的回答。很抱歉他们都是正确的,但因为我只能给出一个正确的答案,所以我把它给了第一个回答的人。对不起。但我给了每个人一票。
【解决方案5】:

试试这个;

Sting text2check = "你的名字":

for(int t = 0; t < array.length; t++)
{
if (text2check.equals(array[t])
// Process it Here
break;
}

【讨论】:

  • 数组上没有可用的字符串
  • 我会编辑我的答案,我想说数组的返回值是字符串,因为你要在其中存储字符串。或者使用 String.valueof(array[t]) 如果它不是 String 已经。
  • 好的,明白了。不需要字符串,它现在可以工作了。
  • 我很抱歉他们都是正确的,但由于我只能给出一个正确的答案,所以我把它给了第一个回答的人。对不起。但我给了每个人一票。
  • 也为了我的目的.equals应该是.contains
【解决方案6】:

“如何检查字符串是否包含数组中存在的特定单词?”数组中是否存在输入字符串包含的元素相同这个元素

Java 8

String[] words = { "example", "hello world" };
String input = "a whole bunch of words";

Arrays.stream(words).anyMatch(input::contains);

(如果需要,也可以提取匹配的单词:)

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

如果您被 Java 7 卡住,您将不得不自己重新实现“anyMatch”和“过滤器”:

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;
}

【讨论】:

  • 谢谢。但现在已经回答了。很抱歉他们都是正确的,但因为我只能给出一个正确的答案,所以我把它给了第一个回答的人。对不起。但我给了每个人一票。
  • 不要抱歉 :) 只需选择对您有帮助的答案,祝您的程序好运!
  • 好的,谢谢。希望我可以将多个答案标记为正确。
  • 所有被投票的答案在某种意义上都是“正确的”,“选择”的答案只是为了让和你有同样问题的人一眼就能找到“任何解决方案”。
  • 是的,但对你们来说仍然有 15 分正确。我很难过,即使你们付出了所有的努力,我也不能把它们给你们。
【解决方案7】:

这将采用一个字符串数组,并在所有字符串中搜索以查找在字符串中找到的特定字符序列。此外,原生 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
    }
}

【讨论】:

  • 我很抱歉他们都是正确的,但由于我只能给出一个正确的答案,所以我把它给了第一个回答的人。对不起。但我给了每个人一票。
  • 也为了我的目的stringArray[i].contains(inputText)应该是inputText.contains(stringArray[i])
  • 是的,我在重新阅读问题时看到了这一点。我以为你想要相反的答案。我的错误
  • 嘿没问题。想要它几乎是正确的,而且您还努力帮助别人对吗?没问题
  • 是的。谢谢
猜你喜欢
  • 2011-05-20
  • 2013-12-23
  • 1970-01-01
  • 2011-07-20
  • 2013-10-09
相关资源
最近更新 更多