String in Java


This program demonstrates various operations that can be performed on strings in Java.

  • The program begins by declaring two string variables, a and b, and initializing them with the values "Tutor Joes" and "tutor Joes", respectively. The program then prints the values of a and b, along with their hash codes, using the System.out.println() method.
  • The program then demonstrates several string methods. It calls the equals() method to compare the values of a and b, the equalsIgnoreCase() method to compare them ignoring case, the length() method to find the length of the string a, and the charAt() method to retrieve the character at a specific index of the string.
  • The program also calls the toUpperCase() and toLowerCase() methods to convert the case of a, the replace() method to replace a substring within a, the contains() method to check if a contains a specific substring, the isEmpty() method to check if a is empty, and the endsWith() and startsWith() methods to check if a ends or starts with a specific substring.
  • The program also demonstrates the substring() method, which can be used to extract a portion of a string, and the toCharArray() method, which converts a string to an array of characters. Finally, the program declares a new string variable c and demonstrates the trim() method, which removes leading and trailing whitespace from a string.
  • When the program is run, it prints various information about the strings a and c, along with the results of the string methods that are called.

Source Code

public class stringsConcept {
    public static void main(String args[])
    {
        //String in Java
        String a="Tutor Joes";
        String b="tutor Joes";
        System.out.println("A : "+a);
        System.out.println("B : "+b);
 
        System.out.println("A HashCode "+a.hashCode());
        System.out.println("B HashCode "+b.hashCode());
        System.out.println("Equals : "+a.equals(b));
        System.out.println("Equals Ignore Case: "+a.equalsIgnoreCase(b));
        System.out.println("Length: "+a.length());
        System.out.println("CharAt: "+a.charAt(0));
        System.out.println("Uppercase: "+a.toUpperCase());
        System.out.println("Lowercase: "+a.toLowerCase());
        System.out.println("Replace: "+a.replace("Joes","Stanley"));
        System.out.println("Contains : " + a.contains("Joes"));
        System.out.println("Empty : " + a.isEmpty());
        System.out.println("EndWith : " + a.endsWith("es"));
        System.out.println("StartWith : " + a.startsWith("Tut"));
        System.out.println("Substring : " + a.substring(5));
        System.out.println("Substring : " + a.substring(0, 5));
        char[] carray = a.toCharArray();
        for(char c : carray){
            System.out.print(c+ "   ");
        }
        String c=" Tutor ";
        System.out.println("Length: "+c.length());
        System.out.println("C:"+c);
        System.out.println("C Trim :"+c.trim());
        System.out.println("C Trim Length:"+c.trim().length());
 
    }
}
 

Output

A 		: Tutor Joes
B 		: tutor Joes
A HashCode  	: -1314220131
B HashCode  	: 987282301
Equals 		: false
Equals Ignore Case: true
Length		: 10
CharAt		: T
Uppercase	: TUTOR JOES
Lowercase	: tutor joes
Replace		: Tutor Stanley
Contains 	: true
Empty 		: false
EndWith 	: true
StartWith 	: true
Substring 	:  Joes
Substring 	: Tutor
T   u   t   o   r       J   o   e   s   
Length		: 7
C		: Tutor
C Trim 		: Tutor
C Trim Length 	: 5
To download raw file Click Here

Basic Programs