Java String class functions
The methods specified below are some of the most commonly used methods of the String
class in Java. We will learn about each method with help of small code examples for better understanding.
charAt()
method
String charAt()
function returns the character located at the specified index.
Output: u
NOTE: Index of a String starts from 0, hence str.charAt(2)
means third character of the String str.
equalsIgnoreCase()
method
String equalsIgnoreCase()
determines the equality of two Strings, ignoring their case (upper or lower case doesn’t matter with this method).
true
indexOf()
method
String indexOf()
method returns the index of first occurrence of a substring or a character. indexOf() method has four override methods:
int indexOf(String str)
: It returns the index within this string of the first occurrence of the specified substring.int indexOf(int ch, int fromIndex)
: It returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.int indexOf(int ch)
: It returns the index within this string of the first occurrence of the specified character.int indexOf(String str, int fromIndex)
: It returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
Example:
2 11 5 -1
NOTE: -1 indicates that the substring/Character is not found in the given String.
length()
method
String length()
function returns the number of characters in a String.
8
replace()
method
String replace()
method replaces occurances of character with a specified new character.
Change Me
substring()
method
String substring()
method returns a part of the string. substring()
method has two override methods.
1. public String substring(int begin);
2. public String substring(int begin, int end);
The first argument represents the starting point of the subtring. If the substring()
method is called with only one argument, the subtring returns characters from specified starting point to the end of original string.
If method is called with two arguments, the second argument specify the end point of substring.
456789 456
toLowerCase()
method
String toLowerCase()
method returns string with all uppercase characters converted to lowercase.
abcdef
toUpperCase()
method
This method returns string with all lowercase character changed to uppercase.
ABCDEF
valueOf()
method
String class uses overloaded version of valueOf()
method for all primitive data types and for type Object.
NOTE: valueOf()
function is used to convert primitive data types into Strings.
35 type of num is: java.lang.String
toString()
method
String toString()
method returns the string representation of an object. It is declared in the Object class, hence can be overridden by any java class. (Object class is super class of all java classes).
This is my car object
Whenever we will try to print any object of class Car, its toString()
function will be called.
NOTE: If we don’t override the toString()
method and directly print the object, then it would print the object id that contains some hashcode.
trim()
method
This method returns a string from which any leading and trailing whitespaces has been removed.
hello
contains()
Method
String contains()
method is used to check the sequence of characters in the given string. It returns true if a sequence of string is found else it returns false.
true false
endsWith()
Method
String endsWith()
method is used to check whether the string ends with the given suffix or not. It returns true when suffix matches the string else it returns false.
true true
format()
Method
String format()
is a string method. It is used to the format of the given string.
Following are the format specifier and their datatype:
Format Specifier | Data Type |
---|---|
%a | floating point |
%b | Any type |
%c | character |
%d | integer |
%e | floating point |
%f | floating point |
%g | floating point |
%h | any type |
%n | none |
%o | integer |
%s | any type |
%t | Date/Time |
%x | integer |
Integer Value: 125 String Value: onlineexamguide Float Value: 125.000000 Hexadecimal Value: 7d Char Value: a
getBytes()
Method
String getBytes()
method is used to get byte array of the specified string.
Â
getChars()
Method
String getChars()
method is used to copy the content of the string into a char array.
Welcom
isEmpty()
Method
String isEmpty()
method is used to check whether the string is empty or not. It returns true when length string is zero else it returns false.
true false
join()
Method
String join()
method is used to join strings with the given delimiter. The given delimiter is copied with each element
Welcome to onlineexamguide.com Date: 23/01/2020 Time: 2:39:10
startsWith()
Method
String startsWith()
is a string method in java. It is used to check whether the given string starts with given prefix or not. It returns true when prefix matches the string else it returns false.
true false false
String Methods List
Method | Description |
---|---|
char charAt(int index) | It returns char value for the particular index |
int length() | It returns string length |
static String format(String format, Object… args) | It returns a formatted string. |
static String format(Locale l, String format, Object… args) | It returns formatted string with given locale. |
String substring(int beginIndex) | It returns substring for given begin index. |
String substring(int beginIndex, int endIndex) | It returns substring for given begin index and end index. |
boolean contains(CharSequence s) | It returns true or false after matching the sequence of char value. |
static String join(CharSequence delimiter, CharSequence… elements) | It returns a joined string. |
static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) | It returns a joined string. |
boolean equals(Object another) | It checks the equality of string with the given object. |
boolean isEmpty() | It checks if string is empty. |
String concat(String str) | It concatenates the specified string. |
String replace(char old, char new) | It replaces all occurrences of the specified char value. |
String replace(CharSequence old, CharSequence new) | It replaces all occurrences of the specified CharSequence. |
static String equalsIgnoreCase(String another) | It compares another string. It doesn’t check case. |
String[] split(String regex) | It returns a split string matching regex. |
String[] split(String regex, int limit) | It returns a split string matching regex and limit. |
String intern() | It returns an interned string. |
int indexOf(int ch) | It returns the specified char value index. |
int indexOf(int ch, int fromIndex) | It returns the specified char value index starting with given index. |
int indexOf(String substring) | It returns the specified substring index. |
int indexOf(String substring, int fromIndex) | It returns the specified substring index starting with given index. |
String toLowerCase() | It returns a string in lowercase. |
String toLowerCase(Locale l) | It returns a string in lowercase using specified locale. |
String toUpperCase() | It returns a string in uppercase. |
String toUpperCase(Locale l) | It returns a string in uppercase using specified locale. |
String trim() | It removes beginning and ending spaces of this string. |
static String valueOf(int value) | It converts given type into string. It is an overloaded method. |
Â