In this article, you are going to learn String Class in Java Programming Language.
A String can be defined as a collection of characters representing an object. For Instance, ‘W3TrainingSchool’ is a string having 16 characters.
To perform certain operations with these strings, Java provides String Class which comes along with the predefined java.lang package. Join Our Java Classes in Gurgaon and know to string implementation over live projects, we will help you to learn everything about Java and its implementation in industry.
Once you have created a string, you cannot change its value, which means string represents a constant value.
HOW TO CREATE STRINGS ?
As we have earlier defined that a string is a collection of characters, so we can say that a string bears a resemblance to an array of characters. Let’s check out the below example.
String str = “W3trainingschool” ;
The above string can also be treated as an array of characters in the following way.
char name [] = { ‘W’, ‘3’, ‘t’, ‘r’, ‘a’, ‘i’, ‘n’, ‘i’, ‘n’, ‘g’, ‘s’, ‘c’, ‘h’, ‘o’, ‘o’, ‘l’ } ;
String str = new String( name) ;
Based on the above resemblance between a string and an array of characters, you can create a string in the following two ways.
[1] USING STRING LITERAL
Syntax:
String str = “xyz” ;
Example:
class StringExample {
public static void main(String [] args) {
String str = “W3Training School” ;
System.out.println(str) ;
}
}
Output:
W3Training School
[2] USING ‘NEW’ KEYWORD
Syntax:
String str = new String(“xyz”) ;
Example:
class StringExample {
public static void main(String [] args) {
String str = new String(“W3Training School”) ;
System.out.println(str) ;
}
}
Output:
W3Training School
HOW TO DETERMINE LENGTH OF A STRING ?
In java programming language, length() method is used to determine the length of a given string whose syntax is given below.
Syntax:
int length()
Example:
class StringLength {
public static void main(String [] args) {
String str = new String(“Welcome to W3Training School”) ;
System.out.print(“Length of the string is =” + str.length()) ;
}
}
Output:
Length of the string is = 28
HOW TO CONCATENATE TWO JAVA STRINGS
Sometimes it is needed to link two strings together so as to simplify and avoid rewriting the output statement. Java provides concat() method to perform this task.
Syntax:
String concat(String s)
Example 1:
class ConcateStrings {
public static void main(String []args) {
String str = “Welcome” ;
str = str.concat(” to W3Training School”) ;
System.out.println(str) ;
}
}
Output 1:
Welcome to W3Training School
Example 2: The operator (+) can also be used to concatenate(link) two strings together.
class ConcateStrings {
public static void main(String []args) {
String str = “W3Training School offers” ;
System.out.println(str + ” \”Digital Marketing\”” + ” Course”) ;
}
}
Output 2:
W3Training School offers “Digital Marketing” Course
We have listed below some of the instance methods used in string class.
|
|
STATIC METHODS USED IN STRING CLASS
|
|
We have provided you the description on string classes used in java programming language. Hope you like this post. For more updates and related information, stay connected to our blogs on java programming language.