Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert char[] arrays to String

Tags:

java

arrays

Let's say I have an array :

char[] chars={'X','X','m','a','t','t','X','X'};

I want to create String with value "matt", so starting from chars' index 2 to 5. Is there a built-in method to achieve this, or I must do looping?

like image 908
Milan Mendpara Avatar asked Feb 02 '12 13:02

Milan Mendpara


2 Answers

Use this String constructor, so

new String(chars,2,4);
like image 89
nos Avatar answered Oct 24 '22 08:10

nos


check http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html there is a String constructor with a offset

i.e. new String(chars,2,4);

like image 29
Sören Titze Avatar answered Oct 24 '22 09:10

Sören Titze