Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

java - 如何检查单个字符是否出现在字符串中?(How can I check if a single character appears in a string?)

In Java is there a way to check the condition:

(在Java中有一种方法来检查条件:)

"Does this single character appear at all in string x"

(“这个单个字符是否出现在字符串x中”)

without using a loop?

(没有使用循环?)

  ask by barfoon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use string.indexOf('a') .

(你可以使用string.indexOf('a') 。)

If the 'a' is present in string , it returns the index(>=0).

(如果string存在'a' ,则返回索引(> = 0)。)

If not, it returns -1.

(如果不是,则返回-1。)

So, a non-negative return value means that 'a' is present in the string .

(因此,非负返回值意味着'a' is present in the string 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...