Java:判断一个字符串中是否存在另一个字符子串以及判断一个字符串中是否存在指定字符
发布日期:2022-03-18 18:19:28 浏览次数:1 分类:技术文章

本文共 1772 字,大约阅读时间需要 5 分钟。

Java:判断一个字符串中包含指定字符子串,判断一个字符串中存在指定字符

字符串的contains方法可以判断一个字符串中是否存在另一个字符子串,示例如下

String Str = "Hello , World .";        if  (Str.contains("Hello")) {
System.out.println("Str contains Hello"); }

函数说明:

public boolean contains(CharSequence s)Returns true if and only if this string contains the specified sequence of char values.Parameters:	s - the sequence to search forReturns:	true if this string contains s, false otherwiseThrows:	NullPointerException - if s is nullSince:	1.5

注意:contains方法的参数要求是实现了CharSequence 接口的类,包括CharBuffer, Segment, String, StringBuffer, StringBuilder,不包括字符


如果你想对单个字符用contains方法,可以这样使用

String Str = "Hello , World .";        if  (Str.contains(""+'H')) {
System.out.println("Str contains H"); }

使用indexOf判断字符串是否中存在指定字符,实现样例如下:

String Str = "Hello , World .";        if  (Str.indexOf('H')!=-1) {
System.out.println("Str contains H"); } //indexOf返回的是字符在字符串中的位置,如果不存在则返回-1

函数说明:

public int indexOf(int ch)Returns the index within this string of the first occurrence of the specified character. If a character with value ch occurs in the character sequence represented by this String object, then the index (in Unicode code units) of the first such occurrence is returned. For values of ch in the range from 0 to 0xFFFF (inclusive), this is the smallest value k such that: this.charAt(k) == ch is true. For other values of ch, it is the smallest value k such that: this.codePointAt(k) == ch is true. In either case, if no such character occurs in this string, then -1 is returned.Parameters:	ch - a character (Unicode code point).Returns:	the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

转载地址:https://zhang0peter.blog.csdn.net/article/details/88639401 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode 771. Jewels and Stones--Java和Python解法--简单
下一篇:解决Python报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年02月29日 12时24分16秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

mysql居左查询abcd_MySql速查手册 2019-04-21
loadrunner 错误: 无法找到 java.exe_LoadRunner错误及解决方法总结 2019-04-21
Java小魔女芭芭拉_沉迷蘑菇不可自拔,黏土人《小魔女学园》苏西·曼芭芭拉 图赏... 2019-04-21
php+mysql记事本_一个简单记事本php操作mysql辅助类创建 2019-04-21
300小时成为java程序员_直击面试现场: Java程序员3轮6小时面试, 成功拿到阿里offer!... 2019-04-21
中国网建java发送短信_短信验证登陆-中国网建提供的SMS短信平台 2019-04-21
隔行变色java代码_jquery入门—选择器实现隔行变色实例代码 2019-04-21
角标越界 Java_【新人求助】利用占位符操作数据库是总是提示数组角标越界是怎么回事 - Java论坛 - 51CTO技术论坛_中国领先的IT技术社区... 2019-04-21
java类中声明log对象_用于Android环境,java环境的log打印,可打印任何类型数据 2019-04-21
db2与mysql编目_DB2编目、联邦数据库 - Goopand's OS Space - OSCHINA - 中文开源技术交流社区... 2019-04-21
atomikosdatasourcebean mysql_SpringBoot2整合JTA组件实现多数据源事务管理 2019-04-21
webpack 入口文件 php,如何实现webpack多入口文件打包配置 2019-04-21
php tire树,Immutable.js源码之List 类型的详细解析(附示例) 2019-04-21
matlab转差频率控制,转差频率控制的异步电机调速系统的研究 2019-04-21
oracle错误1327,Oracle中的PGA监控报警分析(r11笔记第97天) 2019-04-21
php函数内的循环,PHP 循环列出目录内容的函数代码 2019-04-21
oracle树状排序,Oracle树状结构查询 2019-04-21
深度linux内核升级,深度操作系统 2020.11.11 更新发布:内核升级 2019-04-21
sql 拆解函数_SQL入门50题详解(含知识点讲解及代码运行步骤拆解) 2019-04-21
java和python交互 jni_Python基于pyjnius库实现访问java类 2019-04-21