Java String contains()
Java String contains() method to check if a String contains a specified character sequence. This method returns a boolean datatype which is a result in testing if the String contains the characters specified on the method argument in CharSequence object type. The contains() method returns true if and only if this string contains the specified sequence of char values. Syntax:
public boolean contains(CharSequence sequence)
Example
class TestClass{
public static void main (String[] args){
String str = "twinkle twinkle little star";
boolean got = str.contains("little");
System.out.println("String contains 'little' : " + got);
got = str.contains("java");
System.out.println("String contains 'java' : " + got);
}
}
Output:
String contains 'little' : true
String contains 'java' : false
String hashCode()
In the Java programming language, every class implicitly or explicitly provides a hashCode() method, which digests the data stored in an instance of the class into a single hash value (a 32-bit signed integer). The String hashCode value of a Java String is computed as:
- It is fast, to the extent that it probably produces hashes as the CPU can read the String from memory (i.e. you usually can't get better without skipping large parts of the String). It does just one multiply and one add per character in the String.
- For typical sets of random Strings, it produces well-distributed hashes over the entire int range.
class TestClass{
public static void main (String[] args){
String str = new String("Java String Tutorial");
System.out.println("Hashcode is :" + str.hashCode() );
}
}
Output:
Hashcode is :-188391249
Related Topics
- How to Get the Length of a String
- Java String charAt() Method
- String indexOf() method
- Java String replace()
- String Comparison in Java
- Java String substring()
- Java String concat() Method
- Java String split() method
- Convert String to int
- Java StringBuilder Class
- StringTokenizer in Java
- How to convert int to String in Java?