java字符串contain,Java字符串contains()

更新时间:2023-05-10 18:12:35 阅读: 评论:0

java字符串contain,Java字符串contains()
Java String contains() method is ud to check if the string contains specified character quence or not.
Java String contains()⽅法⽤于检查字符串是否包含指定的字符序列。
Java字符串contains() (Java String contains())
Java String contains() method was introduced in java 1.5 relea as a utility method. Java字符串contains()⽅法是Java 1.5发⾏版中作为实⽤程序⽅法引⼊的。 This method returns true if the string contains the specified quence of char values. El it returns fal. 如果字符串包含指定的char值序列,则此⽅法返回true 。 否则返回fal。 String contains() method internally us String indexOf method, below code snippet shows the contains() method definition.public boolean contains(CharSequence s) {
return String()) >= 0;
} String contains()⽅法内部使⽤String indexOf⽅法,下⾯的代码⽚段显⽰了contains()⽅法的定义。 If the argument is null, then NullPointerException is thrown, it’s clear from the above code that there is no
null check for the argument string. For example; "abc".contains(null); will throw below exception stack trace similar to below.Exception in thread "main"
java.lang.NullPointerException
at java.ba/java.ains(String.java:2047)
at com.journaldev.string.JavaStringContains.main(JavaStringContains.java:17) 如果参数为null,则抛出NullPointerException,从上⾯的代码可以清楚地看出,对参数字符串没有null检查。 例如; "abc".contains(null); 将抛出类似于下⾯的异常堆栈跟踪。 Note that string contains() method is ca nsitive, so "abc".contains("A"); will return fal. 请注意,字符串contains()⽅法区分⼤⼩写,因此"abc".contains("A"); 将返回fal 。 The argument of String contains() method is java.lang.CharSequence, so any implementation class are also fine as argument, such as StringBuffer, StringBuilder and CharBuffer. String contains()⽅法的参数是java.lang.CharSequence ,因此任何实现类也可以⽤作参数,例如StringBuffer , StringBuilder和CharBuffer 。
Java String contains()⽅法简单⽰例 (Java String contains() method simple example)
Let’s look at a simple java string contains method example.
让我们看⼀个简单的java字符串包含⽅法⽰例。
package com.journaldev.string;
/**
* Java String contains example
* @author pankaj
*
*/
public class JavaStringContains {
public static void main(String[] args) {
String str = "abc def";
String str1 = "//ab/\\";
System.out.ains("a")); //true
System.out.ains("\\")); //fal
System.out.ains("\\")); //true
System.out.ains("/")); //true
boolean flag = "abc".contains("A"); //fal
}
}
The output of the above program statements are shown in the comments itlf and lf understood.
以上程序语句的输出显⽰在注释本⾝中,并且易于理解。
Java字符串contains()具有不区分⼤⼩写的检查 (Java String contains() with ca innsitive check)
For ca innsitive check, we can change both the strings to either upper ca or lower ca before calling the contents() method.
对于不区分⼤⼩写的检查,可以在调⽤contents()⽅法之前将两个字符串都更改为⼤写或⼩写。
"abc".toLowerCa().contains("A".toLowerCa()); //true
"abc".toUpperCa().contains("A".toUpperCa()); //true
Java String contains()与CharSequence (Java String contains() with CharSequence)
Let’s look at examples of using String contains() method with
StringBuffer,
StringBuilder and
CharBuffer. All the are implementations of
CharSequence
interface. Also note that
CharBuffer is an
abstract class, creating it’s instance is slightly different from other usual class.
让我们看看将String contains()⽅法与StringBuffer , StringBuilder和CharBuffer⼀起使⽤的⽰例。 所有这些都是CharSequence 接⼝的实现。 还要注意, CharBuffer是⼀个抽象类 ,创建它的实例与其他常规类略有不同。
//Java String contains with StringBuffer
StringBuffer sb = new StringBuffer("Ur");
System.out.ains(sb)); //true
//Java String contains with StringBuilder
StringBuilder sb1 = new StringBuilder(sb);
System.out.ains(sb1)); //true
//Java String contains with CharBuffer
CharBuffer cb = CharBuffer.allocate(4);
cb.append('U');cb.append('s');cb.append('e');cb.append('r');
cb.clear();
System.out.println(cb); //Ur
System.out.ains(cb)); //true
That’s all for java string contains method examples. If you are learning java, I would strongly recommend to go through core java tutorial.
Java字符串包含⽅法⽰例就这些了。 如果您正在学习Java,我强烈建议您阅读核⼼Java教程 。
Reference: API Doc
参考: API⽂档

本文发布于:2023-05-10 18:12:35,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/879988.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字符串   包含   指定   检查   输出   程序   实例
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图