Skip to content

String Class In Java Programming Language

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.

  •  char charAt(int index)
  •  int compareTo(String str)
  •  int compareToIgnoreCase(String str)
  •  int codePointAt(int index)
  •  int codePointBefore(int index)
  •  int codePointCount(int beginIndex, int endIndex)
  •  String concat(String str)
  •  boolean contains(CharSequence s)
  •  boolean contentEquals(CharSequence cs)
  •  boolean contentEquals(StringBuffer cs)
  •  boolean equals(Object anObject)
  •  byte[] getBytes(String charsetName)
  •  byte[] getBytes()
  •  int indexOf(String str)
  •  boolean isEmpty()
  •  int length()
  •  boolean matches(String regex)
  •  String toLowerCase()
  •  String toUpperCase()
  •  String trim()
  •  char[] toCharArray()
  •  String[] split(String regex)
  •  String replace(char char1, char char2)
  •  boolean equalsIgnoreCase(String str)
  •  boolean endsWith(String suffix)

STATIC METHODS USED IN STRING CLASS

  • static String copyValueOf(char[] data)
  • static String copyValueOf(char[] data, int offset), int count)
  • static String valueOf(boolean b)
  • static String valueOf(char c)
  • static String valueOf(float f)
  • static String valueOf(double d)
  • static String valueOf(long l)
  • static String valueOf(int i)
  • static String valueOf(object obj)
  • static String valueOf(char[] data)
  • static String format(String format, Object args)

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.

Facebook
Twitter
LinkedIn
Pinterest