Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there

IM doing code in vb.net. I have a single textbox with the label First & Last Name. The structure be as follows.

First & Last Name: TEXTBOX


The first word be taken for First Name and the second word for Last Name.Now my code be as

Dim sFirstLastName As String = Me.FirstLastNameTextBox.Text
       Dim sName() As String = sFirstLastName.Split(" ")
       Dim s As String = sName(0)
       Dim s1 As String
       If sName.Length >= 2 Then
           s1 = sName(1)
       Else
           s1 = ""


If the user supposed "left one space at the beginning of textbox and type 2 words then in the databse FirstNAme be saved as EMPTY and in the second name only be saved"


can anybody be help me
Posted
Updated 8-Jun-11 1:05am
v2

The notes here[^] explain it thus:
If two delimiters are adjacent, or a delimiter is found at the beginning or end of this instance, the corresponding array element contains Empty.

You should check the number of elements returned from the Split() method and discard a first empty one if there are more than two. Alternatively you could trim the text first to purge it of leading and trailing spaces.
 
Share this answer
 
why r u doing that with single textbox i think u should take two text boxes becoz in ur case it saves in a single field in database and it can affect the filter in database when u search the records by first name or last name so i suggest u take 2 instead.
 
Share this answer
 
Comments
Richard MacCutchan 8-Jun-11 8:47am    
Don't use txtspk, unless you are a 12 year old child.
Sachin__Sharma 9-Jun-11 3:17am    
u r a great by making people happy through ur comments

people write comments for helping others not for hurting
dont mind friend but u started first and plz read this
http://www.ruhanisatsangusa.org/noevil.htm
Richard MacCutchan 9-Jun-11 3:31am    
I was suggesting that you use proper words. Using txtspk (u, r, ur etc) in this forum gives the impression that you are childish or stupid or both. Try and make an effort to appear professional in your comments.
Sachin__Sharma 9-Jun-11 4:38am    
ok dear
Sergey Alexandrovich Kryukov 8-Jul-11 2:19am    
Don't use textspeak, don't be ***so rude***!
--SA
Why use split? why not use string simple functions?

VB
TextBox1.Text.Trim()

Dim SpaceIndex As Integer = TextBox1.Text.IndexOf(" ")
MsgBox(SpaceIndex)

Dim First As String = TextBox1.Text.Substring(0, SpaceIndex)
Dim Last As String = TextBox1.Text.Substring(SpaceIndex + 1)

MessageBox.Show(First)
MessageBox.Show(Last)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900