1. concat()

The concat() is used to concatenate one string object into another string. It concatenates the specified string to the end of the string and returns the combined string.

Method Signature

public String concat ( String a)

Example I:
class StringDemo {
public static void main(String args[]) {
String a="Welcome";
a.concat(" Students");//Here first character is blank space
System.out.println(a); }
}

Output: Welcome

Explanation: In the above program reference variable (object) a is pointing to “Welcome” and when we try to change the original string “Welcome” with the help of concat() then in that case a new String object will be created as “Welcome Students” but it is not pointing to any object because it has no reference. That’s why while printing the value of object a we get only “Welcome” message.

Example II:
class StringDemo {
public static void main(String args[]) {
String a="Welcome";
String b=a.concat(" Students");//Here first character is blank space
System.out.println(a);
System.out.println(b); }
}
Output:
Welcome
Welcome Students
Explanation:

In the above program [ String b=a.concat (” Students”);] has been referred by the reference variable b that’s why while printing the value of object b we will get the output “Welcome Students” which was not possible in the first example because the concatenated object created was referred by none.

Example III:
// Concatenating Space among strings
class StringDemo { public static void main(String[] args) { String s1 = "Work"; String s2 = "Workship"; String s = s1.concat(" ").concat("is").concat(" ").concat(s2); System.out.println(s); }
} 
Output:

Work is Workship

Suggestion: Try to run the program by yourself by printing the value of s1 and s2 respectively to better understand the concept.

2. charAt()

charAt() method of Java String class is used to return a particular character located at the specified index position. The specified index which is passed into the charAt() returns the character value at that index.

Index position starts from zero (0) and if any one tries to find the character of the String which is beyond the size of a String array, then it throws an exception as “StringIndexOutOfBoundsException

Method Signature

char charAt (int index)

Example I:
class StringDemo {
public static void main(String args[]) {
String a="INDIA";
char r=a.charAt(4);//returns character therefore stored in character variable
System.out.println(r); } }

Output: A

Explanation with the help of diagram:

Example II:

class StringDemo {
public static void main(String args[]) {
String a="INDIA";
char r=a.charAt(5);
System.out.println(r); } }
Output:

Exception in thread “main” java.lang.StringIndexOutOfBoundsException

Explanation:

a.charAt(5) statement in the above program calls the specified position i.e.index 5 of the given string “INDIA” which is beyond the existing index, where the last index is 4. Therefore it throws an exception named as java.lang.StringIndexOutOfBoundsException.

3. toLowerCase ( ):

toLowerCase ( ) method is used to convert all the letters of the specified String into lowercase letters or in smaller letters. It returns the String in lowercase letter.

Method Signature

String toLowerCase( )

Example I:

class StringDemo {
public static void main(String args[]) {
String a="INDIA";
System.out.println(a.toLowerCase()); } }

Output: india

Example II:
class StringDemo {
public static void main(String args[]) {
String a="india";
String b="INDIA IS GREAT";
String c=a.toLowerCase();
String d=b.toLowerCase();
System.out.println(c);
System.out.println(d); } }

Output:

india
india is great

4. toUpperCase ( )

toUpperCase ( ) method is used to convert all the letters of the specified String into uppercase letters or in capital letters. It returns the String in uppercase letter.

Method Signature

String toUpperCase ( )

Example I:
class StringDemo {
public static void main(String args[]) {
String a="work is worship";
System.out.println(a.toUpperCase()); } }

Output: WORK IS WORSHIP

Example II:
class StringDemo {
public static void main(String args[]) {
String a="labour";
String b="hard";
String c=a.toUpperCase();
String d=b.toUpperCase();
System.out.println(a+" "+b);
System.out.println(c+" "+d); } }
Ouput:
labour hard
LABOUR HARD

5. length ( )

length ( ) method of String class returns the total numbers of character of any specified String. It returns the integer value by calculating the total number of characters of the string.

Method Signature

int length ( )

Example I:

class StringDemo {
public static void main(String args[]) {
String a=new String("Java2success");
String b="Java";
int l1=a.length();
int l2=b.length();
System.out.println("Length of a="+l1);
System.out.println("Length of b="+l2); } }

Output:

Length of a=12
Length of b=4

Example II:

class StringDemo {
public static void main(String args[]) {
String a=new String(" Java2 success ");// 3 blank spaces added
System.out.println("Length of a="+a.length()); } }

Output: Length of a=15

6. trim ( )

trim ( ) method of String class is responsible for removing leading and trailing white spaces of the specified string. It returns the copy of the String with omitting the leading and trailing white spaces.

Method Signature

String trim ( )

Showing the heading and trailing space.

Example I:

class StringDemo {
public static void main(String args[]) {
String a=new String(" Java2 success ");
System.out.println(a);
System.out.println("Length of a="+a.length());
a=a.trim();
System.out.println(a);
System.out.println("Length of a after trim()="+a.length()); } }

Output:

Interview Question: What is difference between == operator and .equals ()