String manipulation refers to the process of modifying or transforming a string of characters in some way. This can involve tasks such as extracting substrings, replacing characters or substrings, converting case, removing whitespace, and more.
In Java, string manipulation is typically performed using various methods of the String
class. These methods allow you to perform a wide range of string manipulation tasks, such as:
Function | Syntax | Description |
---|---|---|
length() | stringName.length() |
Returns the length of the string |
charAt() | stringName.charAt(index) |
Returns the character at the specified index in the string |
concat() | stringName.concat(stringToConcat) |
Concatenates the specified string to the end of the original string |
indexOf() | stringName.indexOf(stringToFind) |
Returns the index of the first occurrence of the specified string within the original string |
lastIndexOf() | stringName.lastIndexOf(stringToFind) |
Returns the index of the last occurrence of the specified string within the original string |
substring() | stringName.substring(startIndex, endIndex) |
Returns a new string that is a substring of the original string, starting at the specified index and ending at the specified index |
replace() | stringName.replace(oldString, newString) |
Returns a new string where all occurrences of the old string in the original string are replaced with the new string |
trim() | stringName.trim() |
Returns a new string with leading and trailing whitespace removed |
toLowerCase() | stringName.toLowerCase() |
Returns a new string with all characters converted to lowercase |
toUpperCase() | stringName.toUpperCase() |
Returns a new string with all characters converted to uppercase |
startsWith() | stringName.startsWith(stringToFind) |
Returns true if the original string starts with the specified string |
endsWith() | stringName.endsWith(stringToFind) |
Returns true if the original string ends with the specified string |
contains() | stringName.contains(stringToFind) |
Returns true if the original string contains the specified string |
isEmpty() | stringName.isEmpty() |
Returns true if the original string is empty |
split() | stringName.split(delimiter) |
Returns an array of strings that are separated by the specified delimiter |
valueOf() | String.valueOf(value) |
Converts the specified value to a string |
format() | String.format(format, args) |
Returns a formatted string based on the specified format and arguments |
compareTo() | stringName.compareTo(anotherString) |
Compares the original string to another string lexicographically |
equals() | stringName.equals(anotherString) |
Returns true if the original string is equal to another string |
hashCode() | stringName.hashCode() |
Returns the hash code of the original string |
Code
public class StringManipulationExample {
public static void main(String[] args) {
String originalString = "Hello, world!";
// length()
int length = originalString.length();
System.out.println(“Length of string: “ + length);
// charAt()
char firstChar = originalString.charAt(0);
System.out.println(“First character: “ + firstChar);
// concat()
String concatenatedString = originalString.concat(” How are you?”);
System.out.println(“Concatenated string: “ + concatenatedString);
// indexOf()
int index = originalString.indexOf(“world”);
System.out.println(“Index of ‘world’: “ + index);
// substring()
String substring = originalString.substring(7);
System.out.println(“Substring starting at index 7: “ + substring);
// replace()
String replacedString = originalString.replace(“world”, “Java”);
System.out.println(“Replaced string: “ + replacedString);
// trim()
String stringWithWhitespace = ” Hello, world! “;
String trimmedString = stringWithWhitespace.trim();
System.out.println(“Trimmed string: “ + trimmedString);
// toLowerCase()
String lowerCaseString = originalString.toLowerCase();
System.out.println(“Lowercase string: “ + lowerCaseString);
// toUpperCase()
String upperCaseString = originalString.toUpperCase();
System.out.println(“Uppercase string: “ + upperCaseString);
// startsWith()
boolean startsWithHello = originalString.startsWith(“Hello”);
System.out.println(“Starts with ‘Hello’: “ + startsWithHello);
// endsWith()
boolean endsWithExclamation = originalString.endsWith(“!”);
System.out.println(“Ends with ‘!’: “ + endsWithExclamation);
// contains()
boolean containsWorld = originalString.contains(“world”);
System.out.println(“Contains ‘world’: “ + containsWorld);
// isEmpty()
boolean isEmpty = originalString.isEmpty();
System.out.println(“Is empty: “ + isEmpty);
// split()
String[] splitString = originalString.split(“, “);
System.out.println(“Split string: “ + Arrays.toString(splitString));
// valueOf()
int value = 42;
String valueAsString = String.valueOf(value);
System.out.println(“Value as string: “ + valueAsString);
// format()
String formattedString = String.format(“The value is %d”, value);
System.out.println(“Formatted string: “ + formattedString);
// compareTo()
String compareTo = “Goodbye, world!”;
int comparison = originalString.compareTo(compareTo);
System.out.println(“Comparison to ‘Goodbye, world!’: “ + comparison);
// equals()
boolean equals = originalString.equals(compareTo);
System.out.println(“Equals ‘Goodbye, world!’: “ + equals);
// hashCode()
int hashCode = originalString.hashCode();
System.out.println(“Hash code: “ + hashCode);
}
}
This program creates a string called originalString
and then demonstrates how to use each of the string manipulation functions in Java to perform various operations on that string. The output of the program is as follows:
Output:
Length of string: 13
First character: H
Concatenated string: Hello, world! How are you?
Index of 'world': 7
Substring starting at index 7: world!
Replaced string: Hello, Java!
Trimmed string: Hello, world!
Lowercase string: hello, world!
Uppercase string: HELLO, WORLD!
Starts with 'Hello': true
Ends with '!': true
Contains 'world': true
Is empty: false
Value as string: 42
Formatted string: The value is 42
Comparison to 'Goodbye, world!': 1
Equals 'Goodbye, world!': false
Hash code: -1880044555