在 Java 6 中查找子字符串

发布于 2024-10-27 21:46:15 字数 369 浏览 6 评论 0原文

我查看了 String Java 6 中的 API,我没有找到任何方法来计算特定子字符串在给定 String 中出现的次数。

例如,我想知道“is”“not”在字符串“noisxxnotyynotxisi”中出现了多少次。

我可以用循环做很长的路,但我想知道是否有更简单的方法。

谢谢。

编辑:我正在使用Java 6。

I looked through the String API in Java 6 and I did not find any method for computing how many times a specific sub-string appears within a given String.

For example, I would like to know how many times "is" or "not" appears in the string "noisxxnotyynotxisi".

I can do the long way with a loop, but I would like to know whether there is a simpler way.

Thanks.

Edit: I'm using Java 6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宫墨修音 2024-11-03 21:46:15

org.apache.commons.lang.StringUtils.countMatches 方法可能是首选方法。

org.apache.commons.lang.StringUtils.countMatches method could be preferred.

椵侞 2024-11-03 21:46:15

在不使用外部库的情况下,您可以在循环中使用 String.indexOf(String str, int fromIndex);


更新 这个例子完全有效。

/**
 * @author The Elite Gentleman
 * @since 31 March 2011
 *
 */
public class Test {

    private static final String STR = "noisxxnotyynotxisi";

    public static int count(String str) {
        int count = 0;
        int index = -1;

        //if (STR.lastIndexOf(str) == -1) {
        //  return count;
        //}

        while ((index = STR.indexOf(str, index + 1)) != -1) {
            count++;
        }

        return count;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(Test.count("is"));
        System.out.println(Test.count("no"));
    }
}

Without using an external library, you can use String.indexOf(String str, int fromIndex); in a loop.


Update This example fully works.

/**
 * @author The Elite Gentleman
 * @since 31 March 2011
 *
 */
public class Test {

    private static final String STR = "noisxxnotyynotxisi";

    public static int count(String str) {
        int count = 0;
        int index = -1;

        //if (STR.lastIndexOf(str) == -1) {
        //  return count;
        //}

        while ((index = STR.indexOf(str, index + 1)) != -1) {
            count++;
        }

        return count;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(Test.count("is"));
        System.out.println(Test.count("no"));
    }
}
生生漫 2024-11-03 21:46:15

您可以这样做,但循环会更快。

String text = "noisxxnotyynotxisinono";
String search = "no";
int count = text.split(search,-1).length-1;

System.out.println(Arrays.toString(text.split(search,-1)));
System.out.println("count= " + count);

正如您所看到的,

[, isxx, tyy, txisi, , ]
count= 5

如果文本以搜索值开始或结束,则这是正确的。 -1 参数阻止它删除尾随分隔符。

您可以使用带有 indexOf() 的循环,这更有效,但并不那么简单。

顺便说一句:Java 5.0 自 2007 年 8 月起就已停产。也许是时候看看 Java 6 了。(尽管文档非常相似)

You can do this, but a loop would be faster.

String text = "noisxxnotyynotxisinono";
String search = "no";
int count = text.split(search,-1).length-1;

System.out.println(Arrays.toString(text.split(search,-1)));
System.out.println("count= " + count);

prints

[, isxx, tyy, txisi, , ]
count= 5

As you can see this is correct if the text starts or ends with the search value. The -1 argument stops it removing trailing seperators.

You can use a loop with indexOf() which is more efficient, but not as simple.

BTW: Java 5.0 has been EOL since Aug 2007. Perhaps its is time to look at Java 6. (though the docs are very similar)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文