String.contains方法的替代方法是什么是不区分大小写的?

时间:2016-09-23 11:51:00

标签: java string contains readline case-sensitive

从文件中读取行作为字符串并且string.contains(“someexamplestring”)将返回区分大小写的字符串的输出。

如果行中有“someExampleString”,则不会返回。

如何以不区分大小写的方式识别字符串?

6 个答案:

答案 0 :(得分:4)

实际上,这是How to check if a String contains another String in a case insensitive manner in Java?

的副本

如果您的要求比较简单并只处理英文字母,您可以按照以下答案进行操作。

你应该string.toLowerCase().contains("someExampleString".toLowerCase());

从Java SE文档中了解有关public String toLowerCase()的更多信息。

此外,正如问题的评论部分中Artur Biesiadowski暗示的那样,请在此处重新进行迭代:

  

关于所有建议toLowerCase / toUpperCase - 的答案   如果你超出ASCII空间 ,请小心。有一些语言   从低到高和向后(或其他方式)不是   是一致的。土耳其人无家可归' i'我想到了:Dotted and dotless I

另外,为了使其更安全,您可以使用其他方法 toLowerCase(Locale.English) 并始终将语言环境覆盖为英语。但是,限制是你不再国际化了。

string.toLowerCase(Locale.English).contains("someExampleString".toLowerCase(Locale.English));

答案 1 :(得分:1)

首先在输入字符串上使用String.toLowerCase(str)方法,然后应用String.contains(str)方法。

答案 2 :(得分:1)

要实现不区分大小写的操作,我们需要将一种格式的字符串设置为大写或小写。toLowerCase'toUpperCase'方法在您想要包含操作时会很有用

toLowerCase中有两种变体。

somestring.toLowerCase();
somestring.toLowerCase(Locale somelocale)

实施例

String case1 = "StackOverFlow";
String case2 = "STACKOverfloW";
bool isSame ;
isSame = case1.toLowerCase().Contains(case2.toLowerCase());
  

请注意,当您想比较两个字符串时,equalsIgnoreCase()会很有用

答案 3 :(得分:1)

您可以使用正则表达式:

str.matches("(?i).*someexamplestring.*")

答案 4 :(得分:0)

您可以toLowerCase()使用您的字符串,然后使用您想要匹配的较低版本的contains进行[]

答案 5 :(得分:0)

您应该将两个字符串转换为小写或大写。然后做手术。

<div class="box"> <h2>Managed IT Solutions</h2>
    
<p>No matter what your IT requirement is, we can help.
Whether you are looking for a fully managed IT service or one tailored to supplement your internal team, we have a solution for you.
</p>
<i class="fa fa-gg"></i>
  <div class="overbox">
    <div class="title overtext"> 
		<ul>
			<li>Tailored IT Support</li>
			<li>Service Desk</li>
			<li>Networking Monitoring</li>
			<li>Managed Disaster Recovery</li>
			<li>IT PRocurement</li>
			<li>Managed Security</li>
			<li>Co-Location Services</li>
			<li>Hosted Services</li>
			<li>Domain Management</li>
			<li>Security Vulnerability Scanning</li>
		</ul>
		    

	</div>
    <div class="tagline overtext"><button type="button" class="btn btn-primary btn-lg btn-block"><a href="#">Find Out More</a></button> </div>
  </div>
</div>


<div class="box"> <h2>Managed IT Solutions</h2>
    
<p>No matter what your IT requirement is, we can help.
Whether you are looking for a fully managed IT service or one tailored to supplement your internal team, we have a solution for you.
</p>
<i class="fa fa-gg"></i>
  <div class="overbox">
    <div class="title overtext"> 
		<ul>
			<li>Tailored IT Support</li>
			<li>Service Desk</li>
			<li>Networking Monitoring</li>
			<li>Managed Disaster Recovery</li>
			<li>IT PRocurement</li>
			<li>Managed Security</li>
			<li>Co-Location Services</li>
			<li>Hosted Services</li>
			<li>Domain Management</li>
			<li>Security Vulnerability Scanning</li>
          <li>Security Vulnerability Scanning</li>
          <li>Security Vulnerability Scanning</li>
          <li>Security Vulnerability Scanning</li>
       
		</ul>
		   

	</div>
     <div class="tagline overtext"><button type="button" class="btn btn-primary btn-lg btn-block"><a href="#">Find Out More</a></button> </div>
  </div>
</div>

<div class="box"> <h2>Managed IT Solutions</h2>
    
<p>No matter what your IT requirement is, we can help.
Whether you are looking for a fully managed IT service or one tailored to supplement your internal team, we have a solution for you.
</p>
<i class="fa fa-gg"></i>
  <div class="overbox">
    <div class="title overtext"> 
		<ul>
			<li>Tailored IT Support</li>
			<li>Service Desk</li>
			<li>Networking Monitoring</li>
			<li>Managed Disaster Recovery</li>

		</ul>
		    

	</div>
    <div class="tagline overtext"><button type="button" class="btn btn-primary btn-lg btn-block"><a href="#">Find Out More</a></button> </div>
  </div>
</div>