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.

package com.javatutorialhq.java.tutorial.string;

/*
 * Example source code to show the use of
 * static method valueOf(char[] data,int offset,int count)
 */

public class StringValueOfDemo {

	public static void main(String[] args) {
		// declare the character array
		char[] data = new char[]{'t','e','s','t','s','t','r','i','n','g'};
		// declaring the index of our char array to start
		int offset = 0;
		// number of characters we are getting on the character array
		// starting from offset argument
		int count = 5;
		// getting the String equivalent of char array data
		String strValue = String.valueOf(data,offset,count);
		// printing the String equivalent of char array
		System.out.println(strValue);
		// change the value of char array
		// convert a string to char array
		data = "test".toCharArray();
		// check if the strValue has been changed by
		// reinitializing the data array
		System.out.println("After reinitialization: "+strValue);

	}

}

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 :

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 15
	at java.lang.String.(Unknown Source)
	at java.lang.String.valueOf(Unknown Source)
	at com.teknoscope.java.tutorial.string.StringValueOfDemo.main(StringValueOfDemo.java:20)

Suggested Reading List :

References :