Java Basics:String Methods-1
Java String contains()
The String contains() method checks whether the specified string (sequence of characters) is present in the string or not.
The syntax of the String contains()
method is:
string.contains(CharSequence ch)
Here, string is an object of the String
class.
The String contains() method checks whether the specified string (sequence of characters) is present in the string or not.
The syntax of the String contains()
method is:
string.contains(CharSequence ch)
Here, string is an object of the String
class.
contains() Parameters
The contains()
method takes a single parameter.
- ch (charSequence) - a sequence of characters
Note: A charSequence
is a sequence of characters such as: String, CharBuffer, StringBuffer etc.
The contains()
method takes a single parameter.
- ch (charSequence) - a sequence of characters
Note: A charSequence
is a sequence of characters such as: String, CharBuffer, StringBuffer etc.
contains() Return Value
- returns true if the string contains the specified character
- returns false if the string doesn't contain the specified character
- returns true if the string contains the specified character
- returns false if the string doesn't contain the specified character
Example 1: Java String contains()
class Main {
public static void main(String[] args) {
String str1 = "Learn Java";
Boolean result;
// check if str1 contains "Java"
result = str1.contains("Java");
System.out.println(result); // true
// check if str1 contains "Python"
result = str1.contains("Python");
System.out.println(result); // false
// check if str1 contains ""
result = str1.contains("");
System.out.println(result); // true
}
}
Here, str.contains("")
gives true
because the empty string is a subset of every other string.
class Main {
public static void main(String[] args) {
String str1 = "Learn Java";
Boolean result;
// check if str1 contains "Java"
result = str1.contains("Java");
System.out.println(result); // true
// check if str1 contains "Python"
result = str1.contains("Python");
System.out.println(result); // false
// check if str1 contains ""
result = str1.contains("");
System.out.println(result); // true
}
}
Here, str.contains("")
gives true
because the empty string is a subset of every other string.
Example 2: Using contains() With if...else
class Main {
public static void main(String[] args) {
String str1 = "Learn Java";
String str2 = "Java";
String str3 = "java";
Boolean result;
// true because "Learn Java" contains "Java"
if (str1.contains(str2)) {
System.out.println(str1 + " contains " + str2);
}
else {
System.out.println(str1 + " doesn't contains " + str2);
}
// contains() is case-sensitive
// false because "Learn Java" doesn't contains "ava"
if (str1.contains(str3)) {
System.out.println(str1 + " contains " + str3);
}
else {
System.out.println(str1 + " doesn't contain " + str3);
}
}
}
Output
Learn Java contains Java
Learn Java doesn't contain java
____________________________
class Main {
public static void main(String[] args) {
String str1 = "Learn Java";
String str2 = "Java";
String str3 = "java";
Boolean result;
// true because "Learn Java" contains "Java"
if (str1.contains(str2)) {
System.out.println(str1 + " contains " + str2);
}
else {
System.out.println(str1 + " doesn't contains " + str2);
}
// contains() is case-sensitive
// false because "Learn Java" doesn't contains "ava"
if (str1.contains(str3)) {
System.out.println(str1 + " contains " + str3);
}
else {
System.out.println(str1 + " doesn't contain " + str3);
}
}
}
Output
Learn Java contains Java Learn Java doesn't contain java
Java String indexOf()
The String indexOf() method returns the index of the first occurrence of the specified character/substring within the string.
The syntax of the String indexOf()
method either
string.indexOf(int ch, int fromIndex)
or
string.indexOf(String str, int fromIndex)
Here, string is an object of the String
class.
indexOf() Parameters
To find the index of a character, indexOf()
takes these two parameters:
- ch - the character whose starting index is to be found
- fromIndex (optional) - if
fromIndex
is passed, thech
character is searched starting from this index
To find the index of the specified substring within the string, indexOf()
takes these two parameters:
- str - the string whose starting index is to be found
- fromIndex (optional) - if
fromIndex
is passed, thestr
string is searched starting from this index
indexOf() Return Value
- returns the index of the first occurrence of the specified character/string
- returns -1 if the specified character/string is not found.
Example 1: Java String indexOf()
// Java String indexOf() with only one parameter
class Main {
public static void main(String[] args) {
String str1 = "Learn Java";
int result;
// getting index of character 'J'
result = str1.indexOf('J');
System.out.println(result); // 6
// the first occurrence of 'a' is returned
result = str1.indexOf('a');
System.out.println(result); // 2
// character not in the string
result = str1.indexOf('j');
System.out.println(result); // -1
// getting the index of "ava"
result = str1.indexOf("ava");
System.out.println(result); // 7
// substring not in the string
result = str1.indexOf("java");
System.out.println(result); // -1
// index of empty string in the string
result = str1.indexOf("");
System.out.println(result); // 0
}
}
Notes:
- The character
'a'
occurs multiple times in the"Learn Java"
string. TheindexOf()
method returns the index of the first occurrence of'a'
(which is 2). - If the empty string is passed,
indexOf()
returns 0 (found at the first position. It is because the empty string is a subset of every substring.
Example 2: indexOf() With fromIndex Parameter
class Main {
public static void main(String[] args) {
String str1 = "Learn Java programming";
int result;
// getting the index of character 'a'
// search starts at index 4
result = str1.indexOf('a', 4);
System.out.println(result); // 7
// getting the index of "Java"
// search starts at index 8
result = str1.indexOf("Java", 8);
System.out.println(result); // -1
}
}
Notes:
- The first occurrence of
'a'
in the"Learn Java programming"
string is at index 2. However, the index of second'a'
is returned whenstr1.indexOf('a', 4)
is used. It is because the search starts at index 4. - The
"Java"
string is in the"Learn Java programming"
string. However,str1.indexOf("Java", 8)
returns -1 (string not found). It is because the search starts at index 8 and there is no"Java"
in"va programming"
.
Comments
Post a Comment