The methods listed below are some of the most frequently used String class methods in Java.
The charAt() function in a string returns the character at the specified index.
class HelloWorld {
public static void main(String[] args) {
String myStr = "Hello";
char result = myStr.charAt(0);
System.out.println(result);
}
}
H
equalsIgnoreCase() checks for equality between two Strings while ignoring their case.
class HelloWorld {
public static void main(String[] args) {
String myStr = "Hello",str="Coder";
boolean result = myStr.equalsIgnoreCase(str);
System.out.println(result);
}
}
false
The index of the first occurrence of a substring or a character is returned by the indexOf() method.
class HelloWorld {
public static void main(String[] args) {
String myStr = "Hello planet earth, you are a great planet.";
System.out.println(myStr.indexOf("e", 5));
}
}
10
The length() function of a String returns the number of characters in it.
class HelloWorld {
public static void main(String[] args) {
String myStr = "Hello planet earth, you are a great planet.";
System.out.println(myStr.length());
}
}
43
The replace() method in a string replaces all occurrences of a character with a new character.
public class Demo {
public static void main(String[] args) {
String str = "coderzpy";
System.out.println(str.replace('c','C'));
}
}
Coderzpy
The substring() method returns a part of the string.
public class Demo {
public static void main(String[] args) {
String str = "ABCDEFGHIJKLMNO";
System.out.println(str.substring(4));
System.out.println(str.substring(4,7));
}
}
EFGHIJKLMNO
EFG
String class uses an overloaded version of valueOf()
method for all primitive data types and for type Object.
public class Demo {
public static void main(String[] args) {
int num = 115;
String s1 = String.valueOf(num); //converting int to String
s1+= "hello";
System.out.println(s1);
System.out.println("type of num : "+s1.getClass().getName());
}
}
115hello
type of num : java.lang.String
This method returns a string that has been stripped of any leading and trailing whitespaces.
public class Demo {
public static void main(String[] args) {
String str = " hello coderzpy ";
System.out.println(str.trim());
}
}
hello coderzpy
the endsWith() method is used to determine whether the string ends with the specified suffix. If the suffix matches the string, it returns true; otherwise, it returns false.
public class Demo {
public static void main(String[] args) {
String a="Hello welcome to Coderzpy.com";
System.out.println(a.endsWith("m"));
System.out.println(a.endsWith("com"));
}
}
true
true
Note: also read about the String class methods in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…