开发者

reducing the length of character array

开发者 https://www.devze.com 2023-03-04 01:51 出处:网络
I have a char array in string of say length开发者_Go百科 8,Its filled with 8 characters.Now i want that it should be reduced to size 5. in C we do it by putting null in end how can we do it injava?In

I have a char array in string of say length开发者_Go百科 8,Its filled with 8 characters.Now i want that it should be reduced to size 5. in C we do it by putting null in end how can we do it in java?


In Java, you normally wouldn't use a character array. You'd just use a String, so it'd be something like this:

String big = "12345678";
String little = big.substring(0, 5);
// little now equals "12345"


String s = "12345678";
char[] c = new char[5];
System.arraycopy(s.toCharArray(), 0, c, 0, 5);

is one way...

0

精彩评论

暂无评论...
验证码 换一张
取 消