查找字符串中所有出现的子字符串

时间:2014-06-18 20:39:14

标签: java string substring find-occurrences

我正在尝试在stacktrace中找到包含包名称的所有行。这就是我所拥有的:

Log.d("result", result);
        ArrayList<String> allOccurences = new ArrayList<String>();
        int lastIndex = 0;
        int count = 0;
        String packageName = context.getPackageName();

        while(lastIndex != -1) {
            lastIndex = result.indexOf(packageName, lastIndex);
            if(lastIndex != -1) {
                String line = result.substring(lastIndex, packageName.length());
                allOccurences.add(line);
                lastIndex += packageName.length();
            }
        }

        for (int i = 0; i < allOccurences.size(); i++) {
            System.out.println(allOccurences.get(i));
        }

如果我删除

String line = result.substring(lastIndex, packageName.length());
allOccurences.add(line);

它运作得很好。我似乎无法弄清楚为什么。 有什么帮助吗?

这是我正在搜索的堆栈跟踪:

06-18 20:43:59.455: D/result(1975): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.madqa/com.madqa.AndroidMADQAActivity}: java.lang.NullPointerException
06-18 20:43:59.455: D/result(1975):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-18 20:43:59.455: D/result(1975):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-18 20:43:59.455: D/result(1975):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-18 20:43:59.455: D/result(1975):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-18 20:43:59.455: D/result(1975):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 20:43:59.455: D/result(1975):     at android.os.Looper.loop(Looper.java:137)
06-18 20:43:59.455: D/result(1975):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 20:43:59.455: D/result(1975):     at java.lang.reflect.Method.invokeNative(Native Method)
06-18 20:43:59.455: D/result(1975):     at java.lang.reflect.Method.invoke(Method.java:511)
06-18 20:43:59.455: D/result(1975):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 20:43:59.455: D/result(1975):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 20:43:59.455: D/result(1975):     at dalvik.system.NativeStart.main(Native Method)
06-18 20:43:59.455: D/result(1975): Caused by: java.lang.NullPointerException
06-18 20:43:59.455: D/result(1975):     at com.madqa.AndroidMADQAActivity.onCreate(AndroidMADQAActivity.java:22)
06-18 20:43:59.455: D/result(1975):     at android.app.Activity.performCreate(Activity.java:5104)
06-18 20:43:59.455: D/result(1975):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-18 20:43:59.455: D/result(1975):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-18 20:43:59.455: D/result(1975):     ... 11 more

包名是“com.madqa”。堆栈跟踪中有3个出现。我想找到它们并打印出它们中的每一个。 (它应该打印com.madqa 3次)

1 个答案:

答案 0 :(得分:2)

substring采用两个索引,而不是索引和长度。您需要将lastIndex添加到packageName.length(),如下所示:

String line = result.substring(lastIndex, lastIndex+packageName.length());

进行此更改后,您的代码应该可以正常运行。

Demo on ideone.