String


Java String


In Java, string is basically an object that represents sequence of char values. An array of characters works same as Java string. For example:

char[] ch={'a','d','i','b'}; is same as: String s="adib"; 

What is String in Java?

Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object.

How to create a string object?

There are two ways to create String object:
  • By string literal  (Example: String s="welcome"; )
  • By new keyword (Example: String s=new String("Welcome");)

Java Program Demonstrating Basic String Methods
public class StringAllMethod {
public static void main(String[] args) {

//declaration & initialization
String s1 = "Adib Mahfuj";
String s2 = new String("Adib mahfuj"); //Using new keyword
String s3 = " Adib Mahfuj ";
String s4 = "Adib Mahfuj Sadik";

//Printing s1 & s2
System.out.println("s1 : "+s1);
System.out.println("s2 : "+s2);

// Declaration and initialization of string array
String[] names = {"Alice", "Bob", "Charlie", "David"};

// Printing elements of the string array using for-each loop
System.out.println("Names:");
for (String name : names) {
System.out.println(name);
}

//Checking length
System.out.println("Length of s1 : "+s1.length());

// Checking Equality ignoring case
//s1.equalsIgnoreCase(s2) compares s1 and s2 without considering case differences.
//Using s1.equals(s2) instead would print "False" due to the case differences.
System.out.println("Equality check ignoring case: " + s1.equalsIgnoreCase(s2));

// Checking if s1 contains a substring
System.out.println("Check if s1 contains 'Mahfuj': " + s1.contains("Mahfuj"));

// Checking if s2 is empty
System.out.println("Check if s2 is empty: " + s2.isEmpty());

// Converting to uppercase
System.out.println("Uppercase of s1 : " + s1.toUpperCase());

// Converting to lowercase
System.out.println("Lowercase of s2 : " + s2.toLowerCase());

// Checking if s1 starts with "Adib"
System.out.println("s1 starts with 'Adib': " + s1.startsWith("Adib"));

// Checking if s2 ends with "mahfuj"
System.out.println("s2 ends with 'mahfuj': " + s2.endsWith("mahfuj"));

// Demonstrating charAt and codePointAt
System.out.println("Character at index 3 in s1: " + s1.charAt(3));
System.out.println("Unicode code point at index 3 in s1: " + s1.codePointAt(3)); //return Ascii value

// Finding index of substring "Mahfuj" in s1
System.out.println("Index of 'Mahfuj' in s1: " + s1.indexOf("Mahfuj"));

// Finding last index of 'i' in s1
System.out.println("Last index of 'i' in s1: " + s1.lastIndexOf('i'));

// Trimming whitespace from s3
System.out.println("Trimmed s2: "+ s3.trim());

// Replace "mahfuj" with "sadik" in s2
String s2Modified = s2.replace("mahfuj", "sadik");
System.out.println("Replacing 'mahfuj' with 'sadik' in s2: " + s2Modified);

// Split s4 by space
String[] parts = s4.split(" ");
System.out.println("Splitting s4 by space: ");
for (String part : parts) {
System.out.println(part);
}
System.out.println();
}
}
Explanation
  • length(): This method returns the length of the string s1, which is the number of characters it contains. In this case, it prints the length of the string "Adib Mahfuj", which is 11.
  • equalsIgnoreCase(): This method compares the string s1 with another string s2, ignoring differences in case. It returns true if both strings are equal when case is ignored, and false otherwise.
  • contains(): Checks if the string s1 contains the specified sequence of characters "Mahfuj". It returns true if s1 contains "Mahfuj", otherwise false.
  • isEmpty(): Checks whether the string s2 is empty. Returns true if s2 is empty, otherwise false.
  • toUpperCase() & toLowerCase(): toUpperCase() converts all characters of the string to uppercase. toLowerCase() converts all characters of the string to lowercase. These methods return new strings with the converted characters.
  • startsWith(): Checks if the string s1 starts with the specified prefix ("Adib" in this case). Returns true if s1 starts with "Adib", otherwise false.
  • endsWith(): Checks if the string s2 ends with the specified suffix ("mahfuj" in this case). Returns true  if s2 ends with "mahfuj", otherwise false.
  • charAt(): Returns the character at the specified index (zero-based) in the string s1.
  • codePointAt(): Returns the Unicode code point value of the character at the specified index in the string s1.
  • indexOf(): Returns the index of the first occurrence of the substring "Mahfuj" within the string s1. Returns -1 if the substring is not found.
  • lastIndexOf(): Returns the index of the last occurrence of the character 'i' within the string s1. Returns -1 if the character is not found.
  • trim(): Removes leading and trailing whitespace (spaces, tabs, etc.) from the string s3. Returns a new string with trimmed whitespace.
  • replace(): Replaces all occurrences of the substring "mahfuj" in the string s2 with the substring "sadik". Returns a new string with the replacements made.
  • split(): Splits the string s4 into an array of substrings using the space character (" ") as a delimiter. Returns an array (parts) containing the substrings.

Comments