java如何判断一个字符串中有几个

如题所述

    判断长度"abc".length()

    判断某个字符有几个

    String a = "abbdfsads";

    int n = a.length()-a.replaceAll("a", "").length();

    System.out.println("字符串中字符a有"+n+"个");

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-31

java根据以下判断一个字符串中有几个,

1、判断长度"abc".length()

2、判断某个字符有几个

String a = "abbdfsads";

int n = a.length()-a.replaceAll("a", "").length();

System.out.println("字符串中字符a有"+n+"个");

/**

* Returns true if and only if this string contains the specified

* sequence of char values.

*

* @param s the sequence to search for

* @return true if this string contains <code>s</code>, false otherwise

* @throws NullPointerException if <code>s</code> is <code>null</code>

* @since 1.5

*/

public boolean contains(CharSequence s) {

return indexOf(s.toString()) > -1;

}


String str = "dfdf点点滴滴";

boolean b1 = str.contains("d");

boolean b2 = str.contains("点点滴滴");

本回答被网友采纳
第2个回答  2017-02-13
"Str".length()
相似回答