字符串.contains() [英] String .contains()
问题描述
大家好,
我发现String java api不包含String.contains(char aChar)方法,我感到很惊讶。它有一个类似于charsequence的方法,但不是我需要的方法。
因此,除非有人告诉我错误,否则我想出另一种方式。
如果我有一个char aChar =''x''
那么一个字符串aString =" test"
如果我这样做aString.indexOf(aChar);
我会得到aChar的索引号还是-1如果它不在那里?
那么如果返回-1,我可以返回false ???
问候
Brendan
Hi all,
I was rather suprised to find that the String java api doesn''t contain String.contains(char aChar) method. It has a similar method for a charsequence but not one for what i need.
So unless someone tells me im wrong I ve come up with a another way.
If i have a char aChar = ''x''
then a String aString = "test"
if i do this aString.indexOf(aChar);
will I either get the index number of aChar or -1 if it isn''t in there??
So then i can return false if -1 is returned???
Regards
Brendan
推荐答案
是的,您也可以使用String作为indexOf()方法的参数,例如
" foobar" .indexOf(" ; oba")== 2
亲切的问候,
Jos
Yep, you can also use a String as an argument for the indexOf() method, e.g.
"foobar".indexOf("oba") == 2
kind regards,
Jos
欢呼Jos,
我开始为自己思考!!
cheers Jos,
I''m starting to think for myself!!
毕竟我不是那么聪明......
aString =" three"
aChar =''e''
由于多次出现,aString.indexOf(aChar)的返回是什么?
I''m not so smart after all....
aString = "three"
aChar = ''e''
What would be the return of aString.indexOf(aChar) because of multiple occurences?
这篇关于字符串.contains()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!