In this page, we will learn about Java Strings, how to create Strings, and there various methods with the help of examples.
In java, String is a sequence of characters, for e.g. “Coding” is a string of 6 characters and it's containing a sequence of characters ' C ' , ' o ' , ' d ' , ' i ' , ' n ' and ' g '. In java, string is an immutable object which means it is constant and can cannot be changed once it has been created.
We use double quotes to represent a string in Java.
// create a string
String name = "Hello Programmer";
Here, we have created a string variable named name. The variable is initialized with the string Hello programmer.
Note: Java strings are not primitive types (like int, char, etc). Instead, all strings are objects of a predefined class named String.
And, all string variables are instances of the String class.
There are two ways to create a String in Java:
1 -> String literal
In java, Strings can be created like this: Assigning a String literal to a String instance:
// create a string
String name1 = "Hello World";
String name2 = "Welcome Programmer";
Create a String in Java
class Simple{
public static void main(String args[]){
// create a string
String name1 = "Java";
String name2 = "JavaScript";
String name3 = "PHP";
System.out.println(name1);
System.out.println(name2);
System.out.println(name3);
}
}
Output:
Java
JavaScript
PHP
2 -> Using New Keyword
So far we have created strings like primitive types in Java.
Since strings in Java are objects, we can create strings using the new keyword as well.
// create a string using the new keyword
String name = new String("Hello Jaction");
In the above example, we have created a string name using the new keyword.
Create Strings in java using the new keyword
class Simple{
public static void main(String args[]){
// create a string using the new keyword
String name = new String("Hello Jaction");
System.out.println(name1);
}
}
Output:
Hello Jaction
A String in Java is actually an object, which contain methods that can perform certain operations on strings. For example, the length of a string can be found with the length() method:
class Simple{
public static void main(String args[]){
// create a string
String name = "Java Programming";
System.out.println("String: " + name);
// get the length of name
int length = name.length();
System.out.println("Length: " + length);
}
}
Output:
String: Java Programming
Length: 16
In the above example, the length() method calculates the total number of characters in the string and returns it.
The + operator can be used between strings to combine them. This is called concatenation:
class Simple{
public static void main(String args[]){
// create first string
String str1 = "Java";
System.out.println("First String: " + str1);
// create second string
String str2 = "Programmer";
System.out.println("Second String: " + str2);
// join str1 and str2
System.out.println(str1 + " " + str2);
}
}
Output:
First String: Java
Secorend String: Programmer
Full String: Java Programmer
Note: that we have added an empty text (" ") to create a space between str1 and str2 on print.
We can also use concat() method to concatenate two strings:. For example,
class Simple{
public static void main(String args[]){
// create first string
String str1 = "Java";
System.out.println("First String: " + str1);
// create second string
String str2 = "Programmer";
System.out.println("Second String: " + str2);
// join str1 and str2
String string = str1.concat(str2);
System.out.println("Full String: " + string);
}
}
Output:
First String: Java
Secorend String: Programmer
Full String: Java Programmer
Here is the list of methods supported by String class:
Methods | Description | Return Type |
---|---|---|
charAt() | Returns the character at the specified index (position) | char |
codePointBefore() | Returns the Unicode of the character before the specified index | int |
codePointAt() | Returns the Unicode of the character at the specified index | int |
contentEquals() | Checks whether a string contains the exact same sequence of characters of the specified CharSequence or StringBuffer | boolean |
contains() | Checks whether a string contains a sequence of characters | boolean |
concat() | Appends a string to the end of another string | String |
compareToIgnoreCase() | Compares two strings lexicographically, ignoring case differences | int |
compareTo() | Compares two strings lexicographically | int |
codePointCount() | Returns the Unicode in the specified text range of this String | int |
getBytes() | Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array | byte[] |
format() | Returns a formatted string using the specified locale, format string, and arguments | String |
equalsIgnoreCase() | Compares two strings, ignoring case considerations | boolean |
copyValueOf() | Returns a String that represents the characters of the character array | String |
endsWith() | Checks whether a string ends with the specified character(s) | boolean |
equals() | Compares two strings. Returns true if the strings are equal, and false if not | boolean |
lastIndexOf() | Returns the position of the last found occurrence of specified characters in a string | int |
isEmpty() | Checks whether a string is empty or not | boolean |
intern() | Returns the canonical representation for the string object | String |
getChars() | Copies characters from a string to an array of chars | void |
hashCode() | Returns the hash code of a string | int |
indexOf() | Returns the position of the first found occurrence of specified characters in a string | int |
replaceFirst() | Replaces the first occurrence of a substring that matches the given regular expression with the given replacement | String |
replace() | Searches a string for a specified value, and returns a new string where the specified values are replaced | String |
regionMatches() | Tests if two string regions are equal | boolean |
length() | Returns the length of a specified string | int |
matches() | Searches a string for a match against a regular expression, and returns the matches | boolean |
replaceAll() | Replaces each substring of this string that matches the given regular expression with the given replacement | String |
startsWith() | Checks whether a string starts with specified characters | boolean |
split() | Splits a string into an array of substrings | String[] |
subSequence() | Returns a new character sequence that is a subsequence of this sequence | CharSequence |
substring() | Returns a new string which is the substring of a specified string | String |
toCharArray() | Converts this string to a new character array | char[] |
toLowerCase() | Converts a string to lower case letters | String |
toString() | Returns the value of a String object | String |
toUpperCase() | Converts a string to upper case letters | String |
trim() | Removes whitespace from both ends of a string | String |
valueOf() | Returns the string representation of the specified value | String |