Do you know all the things you can do with Java Strings?
Introduction
Java strings are specific types of objects of java.lang, which is a class that represents a sequence of characters. For creating and manipulating Strings, the Java platform needs a String class, these are the string functions in java. Strings are mainly used in Java to make the memory of Java more efficient.
What are Strings in Java?
In Java, String does not identify as a Datatype. It identifies as a class. Any programmer can create a String by instantiating the String class that is situated in the package of java.lang. It assists to generate some Methods and construction for creating, manipulating, and searching a String. The objects of Strings are immutable that's why they can not be overwritten. If there are any alterations needed in string operations in java, it creates a new object.
How to Create a String Object?
There are a few steps to create a String Object in Java, those are:
Recommended by LinkedIn
Example
class String_Creation_Demo
{
public static void main(String[] args)
{
String ob1 = new String(); //creates an empty string
//new keyword helps to create an object of class String
System.out.println("Empty String: " +ob1);
char arr[] = {'j','k','a','q','e' };
String ob2 = new String(arr); //String from an array
System.out.println("Contents of Array String: " +ob2);
String ob3 = new String(arr,1,2); //String from subsequence of an array
System.out.println("Contents of subsequence of Array String: " +ob3);
String ob4 = new String(ob3); //String from another string object
System.out.println("Contents of Array String ob4: " +ob4);
}
}
Output
Empty String:
Contents of Array String: jkaqe
Contents of subsequent of Array String: ka
Contents of Array String ob4: ka
Java String Class Methods
Java string methods are important for manipulating the content of a string so that it changes accordingly. There are various methods, which will be discussed shortly in this article