A string is a sequence of characters and considered as a constant because it cannot be changed once it’s created, making it immutable. “xyz” , “ijk” are the examples. In Java, all strings are an instance of String class. However, string buffers support mutable strings( strings that can be changed).
For example
String str = “hi”; System.out.println(str +“buddy”);
String Class provides a set of methods for searching, comparing, extracting and concatenating strings.These are some method that provides Java String Class :
It returns character from the mentioned position.
Program:
public class StringProgram { public static void main (String[]args) { String str = "String"; char ch = str.charAt (4); System.out.println (ch + " at given position in " + str); } }
Output:
It tells which one of them comes first alphabetically.It returns an integer value,
Program
public class StringProgram { public static void main (String[]args) { String str = "apple"; String str1 = "age"; if (str.compareTo (str1) == 0) System.out.println ("identical"); else if (str.compareTo (str1) < 0) System.out.println (str1 + " comes after " + str); else System.out.println (str + " comes after " + str1); } }
Output:
It appends one string to another. It returns a newly created string.
Program:
public class StringProgram { public static void main (String[]args) { String str = "Good "; String str1 = "Morning "; String str2 = str.concat (str1); System.out.println (" New string = " + str2); } }
Output:
It returns true if length() is 0. In other words, It checks whether a string consists of a character or not.
Program:
public class StringProgram { public static void main (String[]args) { String str = ""; if (str.isEmpty ()) System.out.println ("It is empty"); else System.out.println ("It has characters"); } }
Output:
It replaces all the old characters with a new one. It returns a new altered string.
Program:
public class StringProgram { public static void main (String[]args) { String str = "The string methods"; String str1 = str.replace ('t', 'M'); System.out.println ("Old String = " + str + "\n New String = " + str1); } }
Output:
It breaks down a particular string into an array of characters.
Program:
public class StringProgram { public static void main (String[]args) { String str = "Example"; char arr[]; arr = str.toCharArray (); System.out.println (" String = " + str); for (int i = 0; i < arr.length; i++) { System.out.println (arr[i]); } } }
Output:
toLowerCase() turns all the characters in strings to lower case
toUpperCase() converts a string to uppercase.
Program:
public class StringProgram { public static void main (String[]args) { String str = "Example"; System.out.println (" Uppercase = " + str.toLowerCase ()); System.out.println (" Lowercase = " + str.toUpperCase ()); } }
Output:
It represents a string as an object. Each Object that we create is a part of Object class which consists of toString() method and when we try to print the object using System.out.Println method, the compiler uses this predefined toString method and produces hashcode which is not desirable. To make it correct, we have to override this toString method.
Program:
class ABC { String name; int no; public String toString () { return "Name = " + name + " \n number = " + no; } } public class Stringprogram { public static void main (String[]args) { ABC obj = new ABC (); obj.name = "Devine"; obj.no = 11; System.out.println (obj); } }
Output:
It extracts a substring from this string which takes a single argument that is the starting point.
It extracts a substring from the string as per specified range which takes two arguments that are starting points and end points.
Program:
public class StringProgram { public static void main (String[]args) { String str = "This is a string"; String substr = str.substring (10); String substr1 = str.substring (8, 13); System.out.println (" Using substring method with single arguement = " + substr); System.out.println (" Using substring method with two arguements = " + substr1); } }
Output:
10. Trim() It removes all the whitespaces that come before and after the string.
This post was last modified on January 2, 2021