class StringMethods {
public static void main(String args[]) {
int n;
String s = "Java programming", t = "", u = "";
System.out.println(s);
// Find length of string
n = s.length();
System.out.println("Number of characters = " + n);
// Replace characters in string
t = s.replace("Java", "C++");
System.out.println(s);
System.out.println(t);
// Concatenating string with another string
u = s.concat(" is fun");
System.out.println(s);
System.out.println(u);
}
}
Step-by-Step Breakdown:
1. Class and Main Method Declaration:
class StringMethods {
public static void main(String args[]) {
A class StringMethods is declared.
The main is the point at which the program gets executed.
2. String Initialization:
int n;
String s = “Java programming”, t = “”, u = “”;
- A string s is initialized with the value “Java programming”.
- Two more string variables t and u are initialized as empty strings (“”).
- The variable n is declared to hold the length of the string.
3. Print the Original String:
System.out.println(s);
The string s is printed.
Output will be:
Java programming
4. Counting the Length of the String:
n = s.length();
System.out.println(“Number of characters = ” + n);
- The method length() is invoked for the string s. It returns the count of characters within the string. This count also includes spaces.
- The count of Java programming is 17 characters.
- The program prints:
Number of characters = 17
5. Replacing Part of the String:
t = s.replace(“Java”, “C++”);
System.out.println(s);
System.out.println(t);
- The replace() method is invoked on string s. It replaces the substring “Java” with “C++”.
- The result is assigned to t.
- s is printed again, which is still “Java programming” because strings in Java are immutable, meaning the original string is not modified.
- t is printed, which now holds the value “C++ programming”.
The output will be:
Java programming
C++ programming
6. Concatenating Another String:
u = s.concat(” is fun”);
System.out.println(s);
System.out.println(u);
- The concat() method is used to append the string ” is fun” to the original string s. This means to create a new string and store it in u.
- s is again printed, which is “Java programming”.
- u is printed, which now contains the value “Java programming is fun”.
- The above line of console will print the following output.
Java programming
Java programming is fun
Program Output:
Here is the complete output from the program.
Java programming
Number of characters = 17
Java programming
C++ programming
Java programming
Java programming is fun
Summary:
- length(): This method returns the number of characters in a string, including spaces.
- replace(): This method creates a new string where a specified substring is replaced by another substring.
- concat(): This method concatenates (joins) a given string to the end of another string.
- String Is Immutable: String in Java are immutable. Any method, replace(), concat() etc return another string; thus, s itself cannot be modified in the method invocation.