Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy only portion of char[] into String

Tags:

java

string

char

I have an array char[] ch. My question is the following. How can I merge values from ch[2] to ch[7] into a String? I would like to achieve this without looping through the char array. Any suggestions?

Thanks for taking the time to answer my question.

like image 796
Dragan Avatar asked May 09 '13 18:05

Dragan


2 Answers

Use new String(value, offset, count), reference.

Where offset is the starting index and count is your index difference. In your case, it's 7-2=5.

Obviously, value is your character array.

like image 67
Sotirios Delimanolis Avatar answered Nov 14 '22 03:11

Sotirios Delimanolis


You can use one of the other constructors for String

There is one that takes a char array, an offset and a length.

new String(ch, 2, 5);
like image 3
FDinoff Avatar answered Nov 14 '22 03:11

FDinoff