【发布时间】:2019-05-06 12:12:54
【问题描述】:
我想检查输入是只有1个字母还是多于1个, 下面是我的代码
public ResponseEntity<String> moReceiver(@RequestParam("data")String data)
String[]subKeyword=data.split(" ");
String subKwd=subKeyword[1];
if(subKwd =="")
{
System.out.println("One letter");
}
else
{
System.out.println("More than One letter");
}
尝试使用
==""
但不起作用.. :( ,并得到了这个错误 --->索引 1 超出长度 1 的范围
【问题讨论】:
-
你需要从
0算起。 -
@NajibRazak 不是,你怎么想?您在链接的帖子中已经很好地解释了它如何与索引一起使用,请阅读它,谢谢:)。
I assume you got as input array just one item, so length is 1 , but the last - and only valid- index is 0(跨编程语言的通用索引,只有少数索引从 1 开始),所以如果你访问arr[1],你会得到数组越界异常..顺便说一句,永远不要使用==进行字符串比较,.equals()是更好的选择 -
喜欢一个实际的字母还是“一个字母”这个词?