JAVA检查一个字符串是否不包含字符串数组中的子字符串。

huangapple go评论69阅读模式
英文:

JAVA check if a string does not contain a sub string from a string array,

问题

你好,我一定很累,因为我似乎无法让这个工作, 在我的脑海中它是有意义的。 我有一个 String[],里面装满了Android应用程序的名称,我正在尝试在将结果写入 JSONArray 之前对其进行过滤。

public static JSONArray appInfo = new JSONArray();
public static String[] ignoreList = {"Google", "Maps", "Settings", "Phone", "Chrome", "Photos", "Duo", "YouTube", "Gmail", "Contacts", "Drive", "Clock", "Messages", "Files", "Calculator", "File manager", "Samsung", "Gallery", "LastPass", "OnePlus", "Weather"};

public void listApps(Context c) {
    PackageManager pm = c.getPackageManager();
    Intent main = new Intent(Intent.ACTION_MAIN, null);
    main.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> packages = pm.queryIntentActivities(main, 0);

    for (ResolveInfo resolve_info : packages) {
        try {
            String package_name = resolve_info.activityInfo.packageName;
            String app = (String) pm.getApplicationLabel(pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA));

            for (String tempStr : ignoreList) {
                if (!app.contains(tempStr)) {
                    JSONObject json = new JSONObject(); // 创建包含应用信息的临时JSON对象。
                    json.put("app", app);
                    appInfo.put(json);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    try {
        Log.d("Debug", appInfo.toString(4));
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

如果我检查:

if (!app.contains("Google")) {

等等... 那么我的 JSONArray 将不包含带有 "Google" 的应用程序名称,但当我遍历 ignoreList 时,我得到以下来自 ignoreList 而不是不在 ignoreList 中的剩余项的 JSON 输出?而且它不断重复对象? 任何帮助将非常受欢迎。

以下是 JSONArray 中的示例输出,其中包含来自 ignoreList 的重复项:

{
    "app": "Chrome"
},
{
    "app": "Chrome"
},
{
    "app": "Chrome"
},
{
    "app": "Chrome"
},
{
    "app": "Chrome"
},
...
{
    "app": "Maps"
},
{
    "app": "Maps"
},
{
    "app": "Maps"
},
{
    "app": "Maps"
}

希望这可以帮助你解决问题。

英文:

Hello I must be tired as I just can't seem to make this work, in my mind it makes sense. I have a String[] full of Android app names that I am trying to filter out before I write the results to a JSONArray.

 public static JSONArray appInfo = new JSONArray();
public static String[] ignoreList = {&quot;Google&quot;, &quot;Maps&quot;, &quot;Settings&quot;, &quot;Phone&quot;, &quot;Chrome&quot;, &quot;Photos&quot;, &quot;Duo&quot;, &quot;YouTube&quot;, &quot;Gmail&quot;, &quot;Contacts&quot;, &quot;Drive&quot;, &quot;Clock&quot;, &quot;Messages&quot;, &quot;Files&quot;, &quot;Calculator&quot;, &quot;File manager&quot;, &quot;Samsung&quot;, &quot;Gallery&quot;, &quot;LastPass&quot;, &quot;OnePlus&quot;, &quot;Weather&quot;};
public void listApps(Context c) {
PackageManager pm = c.getPackageManager();
Intent main = new Intent(Intent.ACTION_MAIN, null);
main.addCategory(Intent.CATEGORY_LAUNCHER);
List&lt;ResolveInfo&gt; packages = pm.queryIntentActivities(main, 0);
for (ResolveInfo resolve_info : packages) {
try {
String package_name = resolve_info.activityInfo.packageName;
String app = (String) pm.getApplicationLabel(pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA));
for (String tempStr : ignoreList) {
if (!app.contains(tempStr)) {
JSONObject json = new JSONObject(); // Create temp json object with app info.
json.put(&quot;app&quot;, app);
appInfo.put(json);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Log.d(&quot;Debug&quot;, appInfo.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}

If I was to check;

if (!app.contains(&quot;Google&quot;)) {

etc.. Then my JSONArray would contain no app names with "Google" but when I iterate over the ignoreList I get the following JSON out which is from the ignoreList not the remaining items that are not in the ignorList? Also it keeps repeating the objects? Any help would be very welcomed.

    {
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Chrome&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Phone&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Settings&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Google Play Store&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Drive&quot;
},
{
&quot;app&quot;: &quot;Maps&quot;
},
{
&quot;app&quot;: &quot;Maps&quot;
},
{
&quot;app&quot;: &quot;Maps&quot;
},
{
&quot;app&quot;: &quot;Maps&quot;
}

答案1

得分: 1

现在您有两个问题:

  1. 重复问题: 因为您在将app添加到JSON列表时没有中断for循环,所以您会继续迭代ignoreList并向JSON列表添加更多app的实例。为了避免这种情况,您需要添加一个break语句。

  2. 添加应该被忽略的应用程序: 这是因为您在比较appignoreList中的每个实例之前就将app添加到JSON列表中,这看起来有点模糊,也对我来说如此 JAVA检查一个字符串是否不包含字符串数组中的子字符串。 所以我将用示例来解释。

示例:

考虑您的app = "Settings",现在您正在使用for循环将其与列表进行比较,首先将其与"Google"进行比较,它不等于它,因此将其添加到JSON列表中,当您将其与"Maps"进行比较时,情况也是如此,直到您将其与"Settings"进行比较时,此时它不会被添加到JSON列表中,但在达到"Settings"之前,您已经多次添加了它,因此在将app添加到JSON列表之前,您需要等待直到完成整个迭代。我使用了一个boolean来实现这一点。

public void listApps(Context c) {

    PackageManager pm = c.getPackageManager();
    Intent main = new Intent(Intent.ACTION_MAIN, null);
    main.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> packages = pm.queryIntentActivities(main, 0);

    for (ResolveInfo resolve_info : packages) {
        try {
            String package_name = resolve_info.activityInfo.packageName;
            String app = (String) pm.getApplicationLabel(pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA));
            boolean isIgnored = false;

            for (String tempStr : ignoreList) {

                if (app.toLowerCase().contains(tempStr.toLowerCase())) {
                    isIgnored = true;
                    break; // 避免不必要的迭代
                }
            }
            if (!isIgnored) {
                JSONObject json = new JSONObject(); // 使用应用程序信息创建临时JSON对象。
                json.put("app", app);
                appInfo.put(json);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    try {
        Log.d("Debug", appInfo.toString(4));

    } catch (JSONException e) {
        e.printStackTrace();
    }

}
英文:

Now you have two issues:

  1. Repetition issue: because you don't break the for loop whenever you add your app to the JSON list, so you continue iterating
    the ignoreList and add more instances of the app to the JSON
    list. to avoid that you need to add a break statement.

  2. Adding apps that should be ignored: this is because you are adding the app to the JSON list before comparing every
    instance from the ignoreList the app.. it looks vague and also
    to me JAVA检查一个字符串是否不包含字符串数组中的子字符串。 so I will explain with example.

Example:

Consider your app = "Settings", and now you are comparing it to the list using the for loop, so first you compare it to "Google", it doesn't equal it, so add it to the JSON list, the same applies when you compare it to "Maps", and so on until you compare it to "Settings", at this time it won't be added to the JSON list, but you already added it multiple times before you reach to "Settings", so you need to wait until you finish your entire iterations before adding the app to the JSON list. I used a boolean to achieve that.

public void listApps(Context c) {
PackageManager pm = c.getPackageManager();
Intent main = new Intent(Intent.ACTION_MAIN, null);
main.addCategory(Intent.CATEGORY_LAUNCHER);
List&lt;ResolveInfo&gt; packages = pm.queryIntentActivities(main, 0);
for (ResolveInfo resolve_info : packages) {
try {
String package_name = resolve_info.activityInfo.packageName;
String app = (String) pm.getApplicationLabel(pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA));
boolean isIgnored = false;
for (String tempStr : ignoreList) {
if (app.toLowerCase().contains(tempStr.toLowerCase())) {
isIgnored = true;
break; // to avoid unneeded iterations
}
}
if (!isIgnored) {
JSONObject json = new JSONObject(); // Create temp json object with app info.
json.put(&quot;app&quot;, app);
appInfo.put(json);
}
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Log.d(&quot;Debug&quot;, appInfo.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}

huangapple
  • 本文由 发表于 2020年8月9日 06:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63320754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定