By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,640 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,640 IT Pros & Developers. It's quick & easy.

How to..string and array

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
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

Nov 20 '05 #2

P: n/a
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

"Dadox" <pa****@yahoo.com> wrote in message
news:cd**********@bagan.srce.hr...
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

Nov 20 '05 #3

P: n/a
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!
--
Dennis in Houston
"Cor Ligthert" wrote:
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
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


Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.