String Handling Method in Java
What is a String?
Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language.
“String” is a Java platform class that allows you to construct and handle strings.
Creating String:
The normal way of a creating String in Java is to simply write:
String s = “Hello World”;
The compiler constructs a String object with the value "Hello world!" whenever it detects a string phrase in your code.
String objects cannot be made by using the new keyword and a function Object, just like any other object. The String class provides 11 constructors that allow you to set the string's initial value from a variety of sources, including an array of characters.
Another way of a creating String is:
String s = new String (“Hello World”);
Where is String Object stored?
When a string object is generated, it is placed in a pool of Strings known as the String pool, which is then stocked in Java's heap memory.
To be accurate, the Java String class implements three different interfaces: CharSequence, Comparable, and Serializable.
String class is a subclass of the Object class
Because strings are immutable, anytime we change one string a new string is formed. Java provides us with many service classes called StringBuilder and StringBuffer that allow us to generate mutable strings.
As we've seen, String in Java is a class, so it's no surprise that it has a large range of methods connected with it.
String Handling Method
charAt (int index):
This method helps in returning the character value at the particular index.
Example:
public class CharAtSample{
public static void main(String args[]){
String sample = “Hello World”;
char ch1 = sample.charAt(1);
System.out.println(ch1);
}
}
Output:
E
equals (Object obj):
This method returns a Boolean value, indicating if the comparing string matches or not.
Example:
public class EqualsSample{
public static void main(String args[]){
String s1="java";
String s2="java";
String s3="string";
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
}
}
Output:
True
False
compareTo(String certainString):
This is a Comparable interface method that the String class implements. It is used to compare a string to the one currently in use string. As a consequence of the comparison, the method returns a value of 0 or a positive or negative number.
- When both the strings are equal it returns 0.
- If string 1st is less than string 2nd it returns negative.
- If string 1st is greater than string 2nd it returns positive.
Example:
public class CompareToSample{
public static void main(String args[]){
String s1="string ";
String s2="string";
String s3="ring";
String s4="swing";
System.out.println(s1.compareTo(s2));
//0 because both are equal
System.out.println(s1.compareTo(s3));
//1 because "s" is 1 time greater than "r"
System.out.println(s1.compareTo(s4));
// -3 because "t" is 3 times lower than "w"
}
}
Output:
0
1
-3
Length():
This method returns an integer value that shows the full length of the String.
Example:
public class EqualsSample{
public static void main(String args[]){
String s1="helloworld ";
String s2="hello";
System.out.println(s1.length());
// 10 is the length of helloworld string
System.out.println(s2.length());
//5 is the length of hello string
}
}
Output:
10
5
replace(char oldLetter, char newLetter) :
This method returns a new string in which the old character is replaced by the new character in all of the string's occurrences.
Example:
public class EqualsSample{
public static void main(String args[]){
String s1="swap";
String s2 = replace(“w”,”n”);
System.out.println(s2);
}
}
Output:
Snap
toLowerCase():
This method changes all the characters in the strings into lowercase.
Example:
public class ToLowerCaseSample{
public static void main(String args[]){
String s1="HELLO WORLD";
String s2 = s1.toLowerCase(s1);
System.out.println(s2);
}
}
Output:
hello world
toUpperCase():
This method changes all the characters in the strings into upper case.
Example :
public class ToUpperCaseSample{
public static void main(String args[]){
String s1="helloworld ";
String s2 = s1.toUpperCase(s1);
System.out.println(s2);
}
}
Output:
HELLOWORLD
concat(String newString):
This produces a new concatenated string with a new string appended to the end of the previous one. The mark on a string is the new string.
Example:
public class ConcatSample{
public static void main(String args[]){
String s1="Welcome to";
String s2 =”my world”;
String s3 = s1.concat(s2);
System.out.println(s3);
}
}
Output:
Welcome to my world
trim():
This function produces a string that is devoid of the leading and following whitespaces found in the original string.
Example:
public class TrimSample{
public static void main(String args[]){
String s1=" My name is ";
System.out.println(s1 +”:Thomson”);
System.out.println(s1.trim() +”:Thomson”);
}
}
Output:
My name is :Thomson
My name is Thomson
split():
After breaking the input string against the specified regular expression, this method provides a character array.
Example:
public class SplitSample{
public static void main(String args[]){
String s1="Hey I Am John";
String [] s2 = s1.split(“\\s”);
//splits based on whitespace
for(String word: s2){
System.out.println(s2);
}
}
}
Output:
Hey
I
Am
John
valueOf():
This method converts several types to String, such as integer to string, long to string, Boolean to string, float to string, and so on.
Example:
public class ValueOfSample{
public static void main(String args[]){
float value = 89;
String s1 = String.valueOf(value);
System.out.println(s1+”%”);
//concatenating the obtained string
}
}
Output:
89%
These are the various types of String handling program methods in Java. The String class isn't limited to the methods listed above; it contains a slew of other handy methods that can make programming a breeze.