Java String valueOf(char[] data,int offset,int count) method example

HQ » Java Tutorial » java.lang » String » valueOf(char[] data,int offset,int count) method example
Java String valueOf(char[] data,int offset,int count) method example 2018-02-19T13:14:10+00:00

java.lang.String valueOf(char[] data,int offset,int count)

Description :

This java tutorial shows how to use by example the valueOf(char[] data,int offset,int count) method of String class of java.lang package. This method returns a string representation of a subset of character array data. The subset is determined by the the method parameter offset and count.

Method Syntax :

public static String valueOf(char[] data,int offset,int count)

Parameter Input :

DataType Parameter Description
char[] data The character array method parameter we are interested to get the String equivalent
int offset The starting index of the char array to copy
int count The count refers to the number of characters we are interested to get on the data char array starting from the offset specified

Method Returns :

The method valueOf(char[] data) returns a String object which is the String representation of a subset of char[] data as specified by method argument offset and count.

Compatibility Version :

Since the beginning

Exception :

IndexOutOfBoundsException

The indexOutOfBoundsException will be thrown by valueOf() method if the following scenario is encountered:

the sum of offset and count is greater than the length of character array if offset is negative if count is negative

Discussion :

The java method, String valueOf(char[] data) should be accessed statically thus we should do the following String.valueOf(char[] data). This is basically one way to print a character array in subset form. It means to say that we are only getting the characters at specific indexes as specified on the method arguments offset and count.

Java Code Example :

This java example source code demonstrates the use of valueOf(char[] data,int offset,int count) method of String class. On this java code, we have declared a character array, and then we have printed the return String of method valueOf(). As you would noticed on the code, we have reinitialize the data character array into new values to prove our postulate that any change later on our array will not change the initial String value extracted in invoking valueOf(). This is because we take a copy of the string equivalent of character once we invoked the method valueOf().

There’s an interesting method we have used, the String.toCharArray() to convert a String into character array.

Sample Output :

Running the valueOf() with method arguments data,offset and count method example source code of java String class will give you the following output

Java String valueOf(char[] data,int offset,int count)

Java String valueOf(char[] data,int offset,int count)

Exception Scenario :

Suggested Reading List :

References :

 

Contact Info

Singapore

Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close