我刚在方法中找到了一段Java代码:
if (param.contains("|")) {
StringTokenizer st = new StringTokenizer(param.toLowerCase().replace(" ", ""), "|");
if (st.countTokens() > 0) {
...
}
} else {
return myString.contains(param);
}
上述情况中的countTokens
是否可以小于1?
答案 0 :(得分:5)
如果您尝试标记的字符串为空,则可以为空,否则它总是至少为1
示例1:
String myStr = "abcdefg";
StringTokenizer st = new StringTokenizer(myStr, ";");
int tokens = st.countTokens();
System.out.println("Number of tokens: " + tokens);
> "Number of tokens: 1"
示例2:
String myStr = "";
StringTokenizer st = new StringTokenizer(myStr, ";");
int tokens = st.countTokens();
System.out.println("Number of tokens: " + tokens);
> "Number of tokens: 0"
示例3:
String myStr = "abc;defg";
StringTokenizer st = new StringTokenizer(myStr, ";");
int tokens = st.countTokens();
System.out.println("Number of tokens: " + tokens);
> "Number of tokens: 2"
答案 1 :(得分:3)
返回0
:
new StringTokenizer("", "|").countTokens()
new StringTokenizer("|", "|").countTokens()
new StringTokenizer("||||", "|").countTokens()
所以countTokens()
在以下时间返回0
:
String
为空String
仅包含分隔符答案 2 :(得分:1)
看看这个
String param="";
StringTokenizer st = new StringTokenizer(param.toLowerCase().replace(" ", ""), "|");
System.out.println(st.countTokens());
答案是0(零)