java - Swapping names using indexOf and substring and compareTo -


i having trouble understanding how use indexof() , substring() , compareto() methods flip people's first name last name in array.

let's have following:

string[] names = new string[]{"joe bloggs", "sam sunday"}; 

you can use following code swap last name , first name:

for (int i=0; < names.length; i++) {     string somename = names[i];     int spacebetweenfirstandlastname = somename.indexof(" ");     //these next 2 lines of code may off character     //grab characters starting @ position 0 in string not including     //   index of space between first , last name     string firstname = somename.substring(0, spacebetweenfirstandlastname);     //grab characters, start @ character after space , go      //   until end of string     string lastname = somename.substring(spacebetweenfirstandlastname+1);     //now, swap first , last name , put array     names[i] = lastname + ", " + firstname; } 

the string compareto method can used sort names comparing 1 name against another, last name start of string. have @ api here , see if can figure out.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -