Strings In Java
Java Strings are discussed, to better meet the demands of the learners.
Java Strings
A string is used to store text. It consists of a collection of characters surrounded by double quotes.
Assign a value to a String variable:
Example: 
Another Example: 
String Length
Java strings are actually objects that contain methods to perform certain operations on strings.
A string’s length can be found with the length() method, for instance:
Example: 
Example: 
More String Methods
There are a variety of methods in String, for example toUpperCase() and toLowerCase():
Example: 
Another Example: 
Java Strings Character Search
The indexOf() method returns the position of the first instance of a specified text in a string (including whitespace) along with the index (the number) at which it appears:
Example: 
Example: 
A string’s initial position is 0, its second is 1, and its third place is 2.
Java Strings Methods
Methods | Overview |
---|---|
contains() | Checks if a substring exists in the string. |
substring() | Substrings of strings are returned. |
join() | Strings are joined using the delimiter. |
replace() | Specifies a new character to replace the old one. |
replaceAll() | Substrings matching a regex pattern are replaced. |
replaceFirst() | First matching substring should be replaced. |
charAt() | The character present in the specified location will be returned. |
getBytes() | Bytes are converted from the string. |
indexOf() | In a string, this function returns the position of the specified character. |
compareTo() | The dictionary order is used to compare two strings. |
compareToIgnoreCase() | Case differences are ignored when comparing two strings. |
trim() | Removes all leading and trailing whitespaces. |
format() | A formatted string is returned. |
split() | Divides a string into an array. |
toLowerCase() | Lowercases the string. |
toUpperCase() | Uppercases the string |
valueOf() | The specified argument will be returned as a string. |
toCharArray() | This function converts the string into a char array. |
matches() | Matches the string against the regex.. |
startsWith() | This method checks if a string begins with a given string. |
endsWith() | A string is checked to see if it ends with the given string. |
isEmpty() | An empty string is checked or not. |
intern() | This function returns the string’s canonical representation. |
contentEquals() | A string is compared to charSequence to determine if it is equal |
hashCode() | The string’s hash code is returned. |
subSequence() | The string is subsequenced by this method. |
Visit our Java String Methods Reference for a comprehensive list of String methods.
All string methods are covered in the reference, along with descriptions and examples.