Page Contents
Function Description
This method is used to convert the objects and all primitive types of values into a string. It is an overloaded method. Using this valueOf() method primitive types int, long, boolean, character, float, double char array, and objects can be converted into String.
- To convert the passing object into string
- To convert a character into a string
- To convert an array of characters into string
- To convert the array of characters from the offset position to mentioned count into string
- To convert the boolean type value into string
- To convert the int type value into string
- To convert the long type value into a string
- To convert the float type value into string
- To convert the double type value into string
Method Signature
- To convert the passing object into string
public static String valueOf(Object obj) - To convert a character into the string
public static String valueOf(char c) - To convert the array of characters into string
public static String valueOf(char data[]) - To convert the array of characters from the offset position to mentioned count into string
public static String valueOf(char data[], int offset, int count) - To convert the boolean type value into string
public static String valueOf(boolean b) - To convert the int type value into string
public static String valueOf(int i) - To convert the long type value into string
public static String valueOf(long l) - To convert the float type value into string
public static String valueOf(float f) - To convert the double type value into string
public static String valueOf(double d)
Parameter(s)
obj : Object to be converted into string
data: The array of characters to be converted into string
offset: Initial offset value of the subarray
count: Length of the subarray.
b: Boolean value(true/false) to be converted into string
c: Character type to be converted into string
i: Int type value to be converted into string
l: Long type value to be converted into string
f: Float type value to be converted into string
d: Double type value to be converted into string
Return Type
String type value
Exception
Throws IndexOutOfBoundsException if offset is negative, or count is negative, or offset+count is larger than data length.
Examples of Java String Class valueOf() Method
1. To convert passing object into string
import java.util.ArrayList; import java.util.List; public class ExampleStringValueOf { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Round"); list.add("The"); list.add("Tech"); // converting object into String String listString = String.valueOf(list); System.out.println("Converted List object : "+listString); } } Output: Converted List object : [Round, The, Tech]
2. To convert a character into string
public class ExampleStringValueOf { public static void main(String[] args) { char charTest = 's'; // converting char into String String charString = String.valueOf(charTest); System.out.println("Converted a character into string : " + charString); } } Output: Converted a character into string : s
3. To convert array of characters into string
public class ExampleStringValueOf { public static void main(String[] args) { char ch[] = { 's', 't', 'r', 'i', 'n', 'g', 's' }; // converting charArray into String String charArrayString = String.valueOf(ch); System.out.println("Converted charactersArray : " + charArrayString); } } Output: Converted charactersArray : strings
4. To convert the array of characters from the offset position to mentioned count into string
public class ExampleStringValueOf { public static void main(String[] args) { char ch[] = { 's', 't', 'r', 'i', 'n', 'g', 's' }; // converting charArray into String String charArrayString = String.valueOf(ch); System.out.println("Converted character array from offset to given count : " + String.valueOf(ch, 3, 2)); } } Output: Converted character array from offset to given count : in
5. To convert boolean value into string
public class ExampleStringValueOf { public static void main(String[] args) { boolean isTrue = true; // converting boolean into String String booleanString = String.valueOf(isTrue); System.out.println("Converted boolean value : "+booleanString); } } Output: Converted boolean value : true
6. To convert int value into string
public class ExampleStringValueOf { public static void main(String[] args) { int age = 32; // converting int value into String String intString = String.valueOf(age); System.out.println("Converted int value : " + intString); } } Output: Converted int value : 32
7. To convert long value into string
public class ExampleStringValueOf { public static void main(String[] args) { long distance = 320000000L; // converting long value into String String longString = String.valueOf(distance); System.out.println("Converted long value : " + longString); } } Output: Converted long value : 320000000
8. To convert float value into string
public class ExampleStringValueOf { public static void main(String[] args) { float salary = 32000f; // converting float value into String String floatString = String.valueOf(salary); System.out.println("Converted float value : " + floatString); } } Output: Converted float value : 32000.0
9. To convert double value into string
public class ExampleStringValueOf { public static void main(String[] args) { double doubleNumber = 3.23456734544434d; // converting double value into String String doubleString = String.valueOf(doubleNumber); System.out.println("Converted boolean value : " + doubleString); } } Output: Converted boolean value : 3.23456734544434
Other Methods of String Class
Sequence | Method | Description |
1 | char charAt(int index) | This method is used to return the character of the string at specified index. First character is at index 0 while last is at string length-1 . StringIndexOutOfBounds Exception is thrown if valid index value is not passed. |
2 | int length() | Returns the number of characters count in a string. |
3 | static String format(String format, Object… args) | Return the formatted String. It is a static method and formats the string as per passing format and argument(s). |
4 | static String format(Locale l, String format, Object… args) | Return the formatted String. It is a static method and formats the string as per passing format and argument(s) and locale. |
5 | String substring(int beginIndex) | Returns substring from the given index position to the end index of the string. |
6 | String substring(int beginIndex, int endIndex) | Returns substring from the specified begin index to the specified end index. |
7 | boolean contains(CharSequence s) | Returns boolean value(true/false). It searches the given sequence of characters within the string and returns true/false accordingly. Characters will be case sensitive while searching in string. |
8 | static String join(CharSequence delimiter, CharSequence… elements) | Returns joined string. This method joins each passing String using the specified delimiter. It is a static method. |
9 | static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) | Returns the joined string. This method joins each passing string object of an iterate object using the specified delimiter and returns the concatenated string. It is a static method. |
10 | boolean equals(Object another) | Returns boolean value(true/false). It checks the equality of string with the passing object. |
11 | boolean isEmpty() | Returns boolean value(true/false). It returns true if the string is empty otherwise returns false. |
12 | String concat(String str) | Returns the concatenating string. It concatenates the passing string at the end of the string. |
13 | String replace(char old, char new) | Return the replaced string. It replaces all occurrences of the specified character with the new character. |
14 | String replace(CharSequence old, CharSequence new) | Return the replaced string. It replaces all occurrences of the specified Character sequence with the specified new Character sequence. |
15 | String replaceAll(String regex, String replacement) | Returns the replaced string. It replaces all occurrences of the sequence of characters with the given replacement string that are matching with the given regular expression. |
16 | String replaceFirst(String regex, String replacement) | Returns the replaced string. It replaces the first matching sequence of characters with the replacement string that matches with the given regular expression. |
17 | static boolean equalsIgnoreCase(String another) | Returns the boolean (true/false) value. It compares the passing string with string by ignoring the case. It compares the content irrespective of lowercase or uppercase of the character(s). |
18 | String[] split(String regex) | Returns the array of strings resulting from splitting of the string around matches of the given regular expression. String is breaked into strings from all the matching regular expressions. |
19 | String[] split(String regex, int limit) | Returns the array of strings resulting from splitting of the string around matches of the given regular expression. It breaks the string from all the matching regular expression upto the given limit. |
20 | String intern() | Returns an interned string. This method helps to compare two objects using == operator and checks in a string constant pool. it is faster for comparison. |
21 | int indexOf(int ch) | Returns the index value of first occurrence of the specified character within the string |
22 | int indexOf(int ch, int fromIndex) | Returns the index value of first occurrence of the specified character from the substring starting from specified index |
23 | int indexOf(String substring) | Returns the index value of first occurrence of the specified substring within the string. |
24 | int indexOf(String substring, int fromIndex) | Returns the index value of the first occurrence of the specified string within the substring starting from specified index to end of the string index. |
25 | int lastIndexOf(int ch) | Returns the index value of last occurrence of the specified character |
26 | int lastIndexOf (int ch, int fromIndex) | Returns the index value of last occurrence of the specified character, search starts at given fromIndex. |
27 | int lastIndexOf (String str) | Returns the index value of last occurrence of the specified string. |
28 | int lastIndexOf (String str, int fromIndex | Returns the index value of last occurrence of the specified string, searches start from given fromIndex |
29 | String toLowerCase() | Returns all the characters of the string in lowercase . |
30 | String toLowerCase(Locale l) | Returns all the characters of the string in lowercase using specified locale. |
31 | String toUpperCase() | Returns all the characters of the string in uppercase. |
32 | String toUpperCase(Locale l) | Returns all the characters of the string in uppercase using specified locale. |
33 | String trim() | Returns the string after removing the white space from the beginning and end of the string. |
In this article, we learned all about the valueOf() method of String class with examples. Apart from the valueOf() method, we got an idea about other String methods too. We will come up with more articles explaining other String methods with examples. For more details please click on the respective methods link.
Very good post! We will be linking to this particularly great article on our site. Keep up the great writing. Harriett Berkly Grand