string.contains比对中文返回false

时间: 2023-10-29 21:05:56 浏览: 32
`string.contains` 方法在比对中文时会出现问题,因为中文是使用多个字符组成的,而 `contains` 方法是基于字符的比对,所以无法正确匹配中文。为了解决这个问题,可以使用其他方法,例如 `string.indexOf` 方法或正则表达式匹配。请注意,如果要使用正则表达式匹配中文,需要使用 Unicode 编码。
相关问题

string.contains示例

以下是一个示例,演示如何使用`string.contains`方法检查一个字符串是否包含特定的子字符串: ```java String message = "Hello, world!"; if(message.contains("world")) { System.out.println("The message contains the word 'world'"); } else { System.out.println("The message does not contain the word 'world'"); } ``` 在这个例子中,我们首先定义了一个字符串`message`,它包含一条消息。然后,我们使用`message.contains`方法来检查这个字符串是否包含特定的子字符串`"world"`。如果包含,则输出一条消息,否则输出另一条消息。 请注意,`string.contains`方法区分大小写,因此如果您搜索的子字符串的大小写与实际字符串不匹配,则它将不会被找到。如果您需要不区分大小写的搜索,您可以使用`string.toLowerCase`或`string.toUpperCase`方法将字符串转换为统一的大小写,然后对其进行搜索。

String.contains

The method String.contains() is a built-in method in Java that is used to check whether a specific sequence of characters is present in a given string or not. It returns a boolean value true if the specified sequence of characters is found in the given string, otherwise it returns false. Syntax: public boolean contains(CharSequence sequence) Parameters: sequence: A sequence of characters to be searched in the given string. It can be a string, StringBuffer, or StringBuilder. Return Value: The method returns a boolean value that indicates whether the specified sequence of characters is found in the given string or not. It returns true if the sequence is found, otherwise false. Example: String str = "Hello World"; boolean result = str.contains("World"); System.out.println(result); Output: true

相关推荐

优化代码 参考 if (cstemp.combos != null && cstemp.combos.Count > 0) { List<BCD_COMBO_ITEMEntity> _ITEMEntities_Yes = new BCD_COMBO_ITEM().GetItemList( String.Format("COMBOGUID IN ({0})", String.Join(",", cstemp.combos.Select(x => "'" + x.GUID + "'").Distinct()))); if (_ITEMEntities_Yes != null && _ITEMEntities_Yes.Count > 0) { List<BCD_COMBO_ITEMEntity> _ITEMEntities_No = (cstemp.Nocombos != null && cstemp.Nocombos.Count > 0) ? new BCD_COMBO_ITEM().GetItemList( String.Format("COMBOGUID IN ({0})", String.Join(",", cstemp.combos.Select(x => "'" + x.GUID + "'").Distinct()))) : null; string _ITEMEntities_YesString = String.Join(",", _ITEMEntities_Yes.Select(x => x.ITEMGUID ).Distinct()); string _ITEMEntities_NoString = (_ITEMEntities_No != null && _ITEMEntities_No.Count > 0)? String.Join(",", _ITEMEntities_No.Select(x => x.ITEMGUID).Distinct()):""; List<BCB_CHECKSUBEntity> subTempsno = !string.IsNullOrWhiteSpace(_ITEMEntities_NoString) ? _CHECKSUBEntities.Where(x => _ITEMEntities_NoString.Contains(x.ITEMGUID)).ToList() : null; string subTempsnoString = (subTempsno != null && subTempsno.Count > 0) ? String.Join(",", subTempsno.Select(x => x.MAINGUID).Distinct()):""; List < BCB_CHECKMAINEntity > mainsTemps = _CHECKMAINEntitys.ToList().Where(r => ((cstemp.ageb > 0 || cstemp.agee > 0) ? (r.AGE >= cstemp.ageb && r.AGE <= cstemp.agee) : true) && (!string.IsNullOrWhiteSpace(subTempsnoString) ? (cstemp.bothagree == "True" ? subTempsnoString.Contains(r.GUID): !subTempsnoString.Contains(r.GUID)) : true)).ToList(); string mainguidstring = (mainsTemps != null && mainsTemps.Count > 0) ? String.Join(",", mainsTemps.Select(x => x.GUID).Distinct()) : ""; List <BCB_CHECKSUBEntity> subTemps = _CHECKSUBEntities.Where(x => !string.IsNullOrWhiteSpace(x.RESULT) && _ITEMEntities_YesString.Contains(x.ITEMGUID) && mainguidstring.Contains(x.MAINGUID)).ToList(); if (subTemps != null ) { drAuto[cstemp.CSName] = subTemps.Select(x => x.MAINGUID).Distinct().Count(); } } }

最新推荐

recommend-type

Java中实现String.padLeft和String.padRight的示例

本篇文章主要介绍了Java中实现String.padLeft和String.padRight,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

Java中Object.equals和String.equals的区别详解

主要给大家介绍了Java中Object.equals和String.equals的区别,文中通过一个小示例让大家轻松的明白这两者的区别,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
recommend-type

js中字符替换函数String.replace()使用技巧

stringObject.replace(regexp/substr,replacement)参数 描述 regexp/substr 必需。规定子字符串或要替换的模式的 RegExp 对象。 请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被...
recommend-type

string中c_str(),data(),copy(p,n)函数的用法总结

以下是对string中c_str(),data(),copy(p,n)函数的用法进行了详细的介绍,需要的朋友可以过来参考下
recommend-type

android手机应用源码Imsdroid语音视频通话源码.rar

android手机应用源码Imsdroid语音视频通话源码.rar
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。