A string is defined as a series of characters or an array of characters in java. They are present within the double quote.
Create a String:
One way to create a String is to directly declare it within the double quote that is by String Literal:
1 | String s = "SimpleToCode"; |
Like any other object, it is possible to create a string object with the help of a new keyword.
such as:
1 2 | char[] s = {'S','i','m','p','l','e','T','o','C','o','d','e'}; String obj = new String(s); |
Example:
1 2 3 4 5 6 7 8 9 | public class StringTest { public static void main(String args[]) { String s = "Simple To Code"; System.out.println( s ); } } |
Output:
1 | Simple To Code |
Example of string with object in Java:
1 2 3 4 5 6 7 8 9 10 | public class StringTest { public static void main(String args[]) { char[] s = {'S','i','m','p','l','e',' ','T','o',' ','C','o','d','e'}; String obj = new String(s); System.out.println( obj ); } } |
Output:
1 | Simple To Code |
Note that: it also takes blank space as a character.
Also, string objects are immutable that is their values are unchangeable once the object is created.
String Methods:
Java provides Various String Methods for String Operation such as compare(), concat(), equals(), length(), compareTo() and amany more.
List of some string Methods supported by Java are:
- char charAt(int index):
This method returns the character at the specified index. - int compareTo(String secondString):
The method compares the two strings depending upon their Unicode Value. - int compareTo(Object o):
Compares the present String to another object. - String concat(String str):
This method concatenates the String ‘str’ at the end of the string. - boolean equals(Object obj):
This method compares the string to the specified object. - boolean endsWith(String suffix):
This method checks or tests whether the string ends with the specified suffix or not. - boolean startsWith(String prefix):
It checks for the string to have a specified prefix and depending on that it returns a true or false boolean value. - int compareToIgnoreCase(String str):
It compares the string the same as compareTo() but here it ignores the case of the string. - int hashCode():
This method returns the hash code of the string. - int indexOf(int ch):
The index of the first occurrence of the specified character ‘ch’ is returned in the string. - int lastIndexOf(int ch):
This one returns the last occurrence ‘ch’ in the string. - int indexOf(String str):
The index of the first occurrence of the specified substring ‘str’ is returned through this method. - int lastindexOf(String str):
similarly, this method returns the last occurrence of the string ‘str’. - String intern():
This method first searches for the particular string and returns the reference of it otherwise it allocates the memory to the particular string and assigns the reference to it. - int length():
This method returns the length of the string. - char[] toCharArray():
It is used to convert the string to a new array. - String toString():
This already present string is itself return. - String toLowerCase():
It is used to all the characters of the string to lower case by the default locale. - String toUpperCase():
It is used to all the characters of the string to upper case by the default locale. - String trim():
It returns the substring of the original string after omitting leading and trailing white spaces.
There are many more String methods such as, static String copyValueOf(char[] data), static String valueOf(), byte[] getBytes(), boolean matches(String regex), static String copyValueOf(char[] data, int offset, int count), string[] split(String regex) and so on.