Java - Strings Class
Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects.
The Java platform provides the String class to create and manipulate strings.
Creating Strings
The most direct way to create a string is to write –
String greeting = "Hello world!";
Whenever it encounters a string literal in your code, the compiler creates a String object with its value in this case, "Hello world!'.
As with any other object, you can create String objects by using the new keyword and a constructor. The String class has 11 constructors that allow you to provide the initial value of the string using different sources, such as an array of characters.
String Methods
Here is the list of methods supported by String class –
Method | Description | Return Type |
Returns the character at the specified index (position) | char | |
Returns the Unicode of the character at the specified index | int | |
Returns the Unicode of the character before the specified index | int | |
Returns the Unicode in the specified text range of this String | int | |
Compares two strings lexicographically | int | |
Compares two strings lexicographically, ignoring case differences | int | |
Appends a string to the end of another string | String | |
Checks whether a string contains a sequence of characters | boolean | |
Checks whether a string contains the exact same sequence of characters of the specified CharSequence or StringBuffer | boolean | |
Returns a String that represents the characters of the character array | String | |
Checks whether a string ends with the specified character(s) | boolean | |
Compares two strings. Returns true if the strings are equal, and false if not | boolean | |
Compares two strings, ignoring case considerations | boolean | |
format() | Returns a formatted string using the specified locale, format string, and arguments | String |
getBytes() | Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array | byte[] |
getChars() | Copies characters from a string to an array of chars | void |
Returns the hash code of a string | int | |
Returns the position of the first found occurrence of specified characters in a string | int | |
intern() | Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index | String |
Checks whether a string is empty or not | boolean | |
Returns the position of the last found occurrence of specified characters in a string | int | |
Returns the length of a specified string | int | |
matches() | Searches a string for a match against a regular expression, and returns the matches | boolean |
offsetByCodePoints() | Returns the index within this String that is offset from the given index by codePointOffset code points | int |
regionMatches() | Tests if two string regions are equal | boolean |
Searches a string for a specified value, and returns a new string where the specified values are replaced | String | |
replaceFirst() | Replaces the first occurrence of a substring that matches the given regular expression with the given replacement | String |
replaceAll() | Replaces each substring of this string that matches the given regular expression with the given replacement | String |
split() | Splits a string into an array of substrings | String[] |
Checks whether a string starts with specified characters | boolean | |
subSequence() | Returns a new character sequence that is a subsequence of this sequence | CharSequence |
substring() | Extracts the characters from a string, beginning at a specified start position, and through the specified number of character | String |
toCharArray() | Converts this string to a new character array | char[] |
Converts a string to lower case letters | String | |
toString() | Returns the value of a String object | String |
Converts a string to upper case letters | String | |
Removes whitespace from both ends of a string | String | |
valueOf() | Returns the primitive value of a String object | String |
Powered by Froala Editor