String Methods in Java
In this class, We discuss String Methods in Java.
The reader should have prior knowledge of string class in Java. Click Here.
charAt()
Example:
class test
{
public static void main(String args[])
{
String s1=” java”;
char output=s1.charAt(1);
System.out.println(output);
}
}
Output:
a
The above method finds the character present at the position mentioned.
The charAt() method will take the position as input.
The method returns the character present at the position.
indexOf()
Example:
class test
{
public static void main(String args[])
{
String s1=” Java is Top”;
System.out.println(s1.indexOf(‘a’));
}
}
Output: 1
The above method, “indexOf()” is opposite to the “charAt” method.
We give the character as input, and the method returns the index position.
The character ‘a’ is the input, and the output is 1.
The first occurrence of the character position is returned.
We can mention the start index to mention the starting position.
Example:
class test
{
public static void main(String args[])
{
String s1=” Java is Top”;
System.out.println(s1.indexOf(‘a’,2));
}
}
Output: 3
Start searching for the character ‘a’ from the second position.
The output is three.
We can mention the string as input.
Example:
class test
{
public static void main(String args[])
{
String s1=” Java is Top”;
System.out.println(s1.indexOf(“is”));
}
}
Output: 5
The starting index position of the string is returned.
If the string or character is not found, the method returns -1.
compareTo()
The method “compareTo()” is used to compare the strings.
The comparison is done in the lexicographic or dictionary order.
If the first string is less than the second string, then the method returns -1
If the first string is greater than the second string, then the method returns 1.
If the two strings are equal, then the method returns zero.
Example:
class test
{
public static void main(String args[])
{
String s1=” java”;
String s2=” java”;
System.out.println(s1.compareTo(s2));
}
}
Output: 0
compareToIgnoreCase()
The method “compareToIgnoreCase()” is the same as “compareTo”, but ignores the case.
The capital and small characters are considered the same.
concat()
The method “concat()” will concatenate the strings.
The below example shows the concatenation.
Example:
class test
{
public static void main(String args[])
{
String s1=”java”;
String s2=”java”;
System.out.println(s1.concat(s2));
}
}
Output: javajava
contains()
The method “contains()” will check for the required string in the given string.
If the required string is present, then return true else, false.
The below program shows the example.
Example:
class test
{
public static void main(String args[])
{
String s1=”java”;
System.out.println(s1.contains(“ava”));
}
}
Output: true
endsWith()
The method “endsWith()” will check the given string ends with the required string.
If the given string ends with the required string, then return true else, false.
class test
{
public static void main(String args[])
{
String s1=”java”;
System.out.println(s1.endsWith(“va”));
}
}
Output: true
equals()
The method “equals()” will check for the equality of two strings.
If both the strings are equal, then return true else, false.
class test
{
public static void main(String args[])
{
String s1=”java”;
String s2=”java”;
System.out.println(s1.equals(s2));
}
}
Output: true
isEmpty()
The method “isEmpty()” will check for an empty string.
If the string is empty, return true else, false.
isEmpty()
class test
{
public static void main(String args[])
{
String s1=”java”;
String s2=””;
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
}
}
Output: false
true
length()
The method “length()” will return the length of the string.
class test
{
public static void main(String args[])
{
String s1=”java”;
String s2=””;
System.out.println(s1.length());
System.out.println(s2.length());
}
}
Output: 4
0
replace()
The method “replace()” will replace the character with another.
class test
{
public static void main(String args[])
{
String s1=”java”;
System.out.println(s1.replace(‘a’,’o’));
}
}
Output: jovo
substring()
The “substring()” method will return the substring for the given positions.
The method will take start and end index positions.
If one input is given, then considered a start index.
From the start index, the substring is considered till the last position in the string.
Example:
class test
{
public static void main(String args[])
{
String s1=”java is top”;
System.out.println(s1.substring(5));
}
}
Output: is top
The substring is considered before the end position if both index positions are mentioned.
class test
{
public static void main(String args[])
{
String s1=”java is top”;
System.out.println(s1.substring(5, 8));
}
}
Output: is
toLowerCase()
toUpperCase()
The above methods convert to lower and upper case characters in a string.
class test
{
public static void main(String args[])
{
String s1=”Java is Top”;
System.out.println(s1.toLowerCase());
System.out.println(s1.toUpperCase());
}
}
Output:
java is top
JAVA IS TOP
trim()
The method “trim()” will delete the empty spaces before and after the string.
class test
{
public static void main(String args[])
{
String s1=” Java is Top “;
System.out.println(s1.trim());
}
}
toCharArray()
The method”toCharArray()” will convert the string to an array of characters.
Example:
class test
{
public static void main(String args[])
{
String s1=”Java is Top”;
char a[]=s1.toCharArray();
System.out.println(a[0]);
}
}
Output: J