Java Strings with Example
In Java, a String
is an immutable sequence of characters that is commonly used to represent text. It is one of the most frequently used classes in the Java language, and it provides many useful methods for manipulating and working with strings. Strings are created using string literals or the String
class, and they can be concatenated using the +
operator. The String
class also provides methods for accessing the length of a string, getting a character at a specific index, comparing strings for equality or lexicographic ordering, and performing other common string operations.
Java Strings
In Java, a String
is an immutable sequence of characters that is commonly used to represent text. It is one of the most frequently used classes in the Java language, and it provides many useful methods for manipulating and working with strings.
Strings are created using string literals or the String
class. A string literal is a sequence of characters enclosed in double quotes, such as "hello"
. When a string literal is used, the Java compiler automatically creates a String
object with the given value.
String str1 = "hello";
You can also create a String
object using the String
class and the new
operator.
String str2 = new String("hello");
Strings are immutable, which means that once a String
object is created, its value cannot be changed. If you need to modify the value of a string, you can create a new String
object with the modified value.
String str3 = str1 + ", world"; // creates a new String object with the value "hello, world"
Java String Methods With Example
Here is a table of some common String
methods in Java, along with examples of how they can be used:
Method | Description | Example |
---|---|---|
length() | Returns the length of the string | int len = str.length(); |
charAt(int index) | Returns the character at the specified index | char c = str.charAt(0); |
equals(Object obj) | Returns true if the strings are equal, false otherwise | boolean b = str1.equals(str2); |
compareTo(String str) | Compares two strings lexicographically and returns an integer value | int i = str1.compareTo(str2); |
substring(int beginIndex) | Returns a new string that is a substring of the original string, starting at the specified index | String sub = str.substring(2); |
substring(int beginIndex, int endIndex) | Returns a new string that is a substring of the original string, between the specified indices | String sub = str.substring(2, 5); |
indexOf(int ch) | Returns the index of the first occurrence of the specified character in the string | int i = str.indexOf('o'); |
indexOf(String str) | Returns the index of the first occurrence of the specified string in the original string | int i = str.indexOf("world"); |
replace(char oldChar, char newChar) | Returns a new string with all occurrences of the specified character replaced with the new character | String newStr = str.replace('o', 'a'); |
length()
: Returns the length of the string, which is the number of characters it contains.charAt(int index)
: Returns the character at the specified index in the string.equals(Object obj)
: Returnstrue
if the two strings are equal, andfalse
otherwise.compareTo(String str)
: Compares the two strings lexicographically and returns an integer value: a value less than 0 if the first string is lexicographically less than the second string, 0 if the strings are equal, and a value greater than 0 if the first string is lexicographically greater than the second string.substring(int beginIndex)
: Returns a new string that is a substring of the original string, starting at the specified index and going to the end of the original string.substring(int beginIndex, int endIndex)
: Returns a new string that is a substring of the original string, between the specified indices.indexOf(int ch)
: Returns the index of the first occurrence of the specified character in the string, or -1 if the character is not found.indexOf(String str)
: Returns the index of the first occurrence of the specified string in the original string, or -1 if the string is not found.replace(char oldChar, char newChar)
: Returns a new string with all occurrences of the specified character replaced with the new character.
The String
class provides many other methods for manipulating strings, such as toLowerCase
, toUpperCase
, and trim
. You can learn more about these methods in the Java documentation.
String methods in java with examples
Here are some examples of how common String
methods in Java can be used, along with a brief explanation of what each method does:
public class Main { public static void main(String[] args) { String str = "hello, world"; // get the length of the string int len = str.length(); // len is 12 // get a character at a specific index in the string char c = str.charAt(0); // c is 'h' // compare two strings for equality boolean b = str.equals("hello, world"); // b is true // compare two strings for lexicographic ordering int i = str.compareTo("hello, world"); // i is 0 // get a substring of the original string String sub = str.substring(2, 5); // sub is "llo" // find the index of a specific character in the string int index = str.indexOf('o'); // index is 4 // replace all occurrences of a specific character in the string String newStr = str.replace('o', 'a'); // newStr is "halla, warld" } }
The length()
method returns the length of the string, which is the number of characters it contains. The charAt(int index)
method returns the character at the specified index in the string. The equals(Object obj)
method returns true
if the two strings are equal, and false
otherwise. The compareTo(String str)
method compares the two strings lexicographically and returns an integer value: a value less than 0 if the first string is lexicographically less than the second string, 0 if the strings are equal, and a value greater than 0 if the first string is lexicographically greater than the second string. The substring(int beginIndex)
method returns a new string that is a substring of the original string, starting at the specified index and going to the end of the original string. The substring(int beginIndex, int endIndex)
method returns a new string that is a substring of the original string, between the specified indices. The indexOf(int ch)
method returns the index of the first occurrence of the specified character in the string, or -1 if the character is not found. The replace(char oldChar, char newChar)
method returns a new string with all occurrences of the specified character replaced with the new character.