How to..string and array

  • Thread starter Thread starter Dadox
  • Start date Start date
D

Dadox

Hi!

If I enter a name in textbox ("John Doe") for example,
how do I remove "John" If I click on command button to do so.
The point is to cut off first name and save it to array so only a last name
stays in textbox.

tnx
 
All roughly typed
\\\
Dim myTextbox.text = myTextbox.Text.Replace("John ","")
///
However when you do not know John and you want the last word,

\\\
Dim myStrings() = myTextbox.Text.Split(" ")
myTextbox.text = myStrings(MyStrings.length-1)
///

I hope this helps?

Cor
 
Dadox,
There are any number of ways to do this, I normally use something like:

Dim text As String = "John Doe"
Dim save As String
Dim index As Integer = text.IndexOf(" "c)
If index = -1 Then
save = String.Empty
Else
save = text.Substring(0, index)
text = text.Substring(index + 1)
End If

You may want to consider using Trim on text before the IndexOf and on text
after the SubString to removing leading & trailing spaces.

text = text.Trim()
text = text.TrimStart(" "c)
Dim index As Integer = text.IndexOf(" "c)

text = text.Substring(index + 1).Trim()

Trim or TrimStart can be used for leading spaces...

Hope this helps
Jay
 
Just a comment: If you are relying on the user to type only the first and last names in that order, you probably will encounter many like my wife (whom I used for testing out new programs) will type last name first then several spaces then maybe some kitchen utensil then maybe the first name. I'd suggest two text boxes for first and last name. Just a comment!
 
Back
Top