String Class In Java

What is String in Java?

String is basically an sequence of char values in Java. String is also an object in Java.

//Declaration of char array

char[] seqChar = {‘q’,’a’,’c’,’r’,’e’,’a’,’t’,’o’,’r’,’s’};
1
2
3
//Declaration of char array

char[] seqChar = {‘q’,’a’,’c’,’r’,’e’,’a’,’t’,’o’,’r’,’s’};
1 St way : Creating the String objects using object creation process using new keyword.

//we can make all this array of characters into a single word.
Instead of separating as characters.
String str = new String(seqChar);

2nd way : We can declare the string like other data types by declaring

String str = “qacreators”

Java String provides many methods to perform operations like substring(), concat(), length(), equals(), replace(), split() etc.

1.SubString() : SubString method in java will separate a part of String from the actual String and will store the separated String.
SubString Example :

OUTPUT :

Java SubString Inbuilt Function

  1. concat() : Concat method will append one string to another String. Second String will be concatenated at the end.

Example of Concat :

OUTPUT :

Java concatenate Inbuilt Function

3.length() : Length method will return total number of characters available in the String.

Example of Length:

OUTPUT :

Java length Inbuilt Function

  1. equals() : Equals method will compare two Strings for their content. If both the String content are equal true value is returned, else false value is returned. Return type is boolean for equals method.

Example of Equals:


OUTPUT :

Java Equals Inbuilt Function

  1. replace() : Replace method will return a String replacing all the old characters with new characters.

Example of Replace:


OUTPUT :

Java Replace Inbuilt Function
6.split() : Split method will split the String based on the regular expression that is passed and will return the char Array.

Example of Split:

OUTPUT :

Leave a Comment

Your email address will not be published. Required fields are marked *