Java String Handling
A string is a sequence of characters surrounded by double quotations. In a java programming language, a string is the object of a built-in class String.
In the background, the string values are organized as an array of a character data type.
The string created using a character array can not be extended. It does not allow to append more characters after its definition, but it can be modified.
Let's look at the following example java code.
char[] name = {'J', 'a', 'v', 'a', ' ', 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's'};
//name[14] = '@'; //ArrayIndexOutOfBoundsException
name[5] = '-';
System.out.println(name);
The String class defined in the package java.lang package. The String class implements Serializable, Comparable, and CharSequence interfaces.
The string created using the String class can be extended. It allows us to add more characters after its definition, and also it can be modified.
Let's look at the following example java code.
String siteName = "btechsmartclass.com";
siteName = "www.btechsmartclass.com";
Creating String object in java
In java, we can use the following two ways to create a string object.
- Using string literal
- Using String constructor
Let's look at the following example java code.
String title = "Java Tutorials"; // Using literals
String siteName = new String("www.btechsmartclass.com"); // Using constructor
🔔 The String class constructor accepts both string and character array as an argument.
String handling methods
In java programming language, the String class contails various methods that can be used to handle string data values. It containg methods like concat( ), compareTo( ), split( ), join( ), replace( ), trim( ), length( ), intern( ), equals( ), comparison( ), substring( ), etc.
The following table depicts all built-in methods of String class in java.
Method | Description | Return Value |
---|---|---|
charAt(int) | Finds the character at given index | char |
length() | Finds the length of given string | int |
compareTo(String) | Compares two strings | int |
compareToIgnoreCase(String) | Compares two strings, ignoring case | int |
concat(String) | Concatenates the object string with argument string. | String |
contains(String) | Checks whether a string contains sub-string | boolean |
contentEquals(String) | Checks whether two strings are same | boolean |
equals(String) | Checks whether two strings are same | boolean |
equalsIgnoreCase(String) | Checks whether two strings are same, ignoring case | boolean |
startsWith(String) | Checks whether a string starts with the specified string | boolean |
endsWith(String) | Checks whether a string ends with the specified string | boolean |
getBytes() | Converts string value to bytes | byte[] |
hashCode() | Finds the hash code of a string | int |
indexOf(String) | Finds the first index of argument string in object string | int |
lastIndexOf(String) | Finds the last index of argument string in object string | int |
isEmpty() | Checks whether a string is empty or not | boolean |
replace(String, String) | Replaces the first string with second string | String |
replaceAll(String, String) | Replaces the first string with second string at all occurrences. | String |
substring(int, int) | Extracts a sub-string from specified start and end index values | String |
toLowerCase() | Converts a string to lower case letters | String |
toUpperCase() | Converts a string to upper case letters | String |
trim() | Removes whitespace from both ends | String |
toString(int) | Converts the value to a String object | String |
split(String) | splits the string matching argument string | String[] |
intern() | returns string from the pool | String |
join(String, String, ...) | Joins all strings, first string as delimiter. | String |
Let's look at the following example java code.
public class JavaStringExample {
public static void main(String[] args) {
String title = "Java Tutorials";
String siteName = "www.btechsmartclass.com";
System.out.println("Length of title: " + title.length());
System.out.println("Char at index 3: " + title.charAt(3));
System.out.println("Index of 'T': " + title.indexOf('T'));
System.out.println("Last index of 'a': " + title.lastIndexOf('a'));
System.out.println("Empty: " + title.isEmpty());
System.out.println("Ends with '.com': " + siteName.endsWith(".com"));
System.out.println("Equals: " + siteName.equals(title));
System.out.println("Sub-string: " + siteName.substring(9, 14));
System.out.println("Upper case: " + siteName.toUpperCase());
}
}
When we run this code, it produce the following output.