vb.net - How can I display a name in a different sequence using strings? -


i'm having trouble problem assigned visual basic 2012 class. instructions below. far, have displaying first name entered, , nothing else. how can make display both first name , last name in sequence requested?

string problem: enter first , last name in text box. take name , display in label box showing last name, first name.

text box entry: jane doe

label box: doe, jane

the code have far below. help!

private sub btndisplay_click(sender object, e eventargs) handles btndisplay.click     dim fullname string     dim firstname string     dim indexnum integer     dim lastname string      fullname = fulltextbox.text      indexnum = fullname.indexof(" ")      firstname = fullname.substring(0, indexnum)      firstlabel.text = firstname     fulltextbox.focus() end sub  private sub fulltextbox_textchanged(sender object, e eventargs)     firstlabel.text = string.empty      fulltextbox.selectall() end sub  private sub btnexit_click(sender object, e eventargs) handles btnexit.click     me.close() end sub 

you on right track first determining space located:

indexnum = fullname.indexof(" ") 

now based on index, can split string 2 strings, firstname , lastname:

firstname = fullname.substring(0, indexnum) lastname = fullname.substring(indexnum+1) 

you need use indexnum+1, instead of indexnum, otherwise include spacing character.

finally group them again using string concatenation operator (&):

firstlabel.text = lastname & ", " & firstname 

the final method looks like:

private sub btndisplay_click(sender object, e eventargs) handles btndisplay.click     dim fullname string     dim firstname string     dim indexnum integer     dim lastname string      fullname = fulltextbox.text      indexnum = fullname.indexof(" ")      firstname = fullname.substring(0, indexnum)     lastname = fullname.substring(indexnum+1)      firstlabel.text = lastname & ", " & firstname     fulltextbox.focus() end sub 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -