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
atai
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 pankajuikit
*
*/
public class JavaStringContains {
square是什么意思
public static void main(String[] args) {
String str = "abc def";
String str1 = "//ab/\\";
System.out.ains("a")); //true
System.out.ains("\\")); //fal
rotateSystem.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.abs130 torrent
以上程序语句的输出显⽰在注释本⾝中,并且易于理解。
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()⽅法之前将两个字符串都更改为⼤写或⼩写。
leap year"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
bottom line参考: API⽂档