What will be the output of the program?
public class Trial41
{
public static void main(String[] args)
{
String csv = "a,b,c";
char[] chars = csv.toCharArray();
System.out.println(chars.length + " " + new String(chars, 0, 3));
}
}
Answer: A
toCharArray gives one entry per character including the separators, so the length is 5. Building a String from offset 0 for a count of 3 takes the first three characters, a comma b. Note the second argument is a count, not an end index, unlike substring.
Please log in or register to report this question.