如何检查字符串中是否出现单个字符? [英] How can I check if a single character appears in a string?

查看:126
本文介绍了如何检查字符串中是否出现单个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这个单个字符是否出现在字符串x


$

解决方案

您可以使用 string.indexOf( 'a')


如果'a'存在于 string 中,它返回索引(> = 0)。如果不是,则返回-1。因此,非负返回值意味着'a'存在于字符串中。



In Java is there a way to check the condition:

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

without using a loop?

解决方案

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

If the 'a' is present in string, it returns the index(>=0). If not, it returns -1. So, a non-negative return value means that 'a' is present in the string.

这篇关于如何检查字符串中是否出现单个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆