Rearranging a string in Java -
i have string of form "firstname middleinitial lastname".
i want convert "lastname, firstname middleintial"
some names may have middle initial, may not:
string name1 = "john papa p"; string name2 = "michael jackson"; // desired output result1 = "papa, john p"; result2 = "jackson, michael";
how can accomplish this?
maybe this?
public class helloworld{ public static void main(string []args){ string name1 = "john papa p"; string name2 = "michael jackson"; string[] split = name1.split(" "); string result; if (split.length > 2) { result = split[1] + ", " + split[0] + " " + split[2]; } else { result = split[1] + ", " + split[0]; } system.out.println(result); } }
java string
No comments:
Post a Comment