|
-
Apr 28th, 2004, 01:06 PM
#1
Thread Starter
New Member
String Manipulation ( to flip names in textbox)
I have text box to enter a name such as Carol Smith
then you click the display new name button and it will write the name as Smith, Carol
I dont if know if that char = 32 is even possible? I thought that was ascii code for a blank space? I am so confused and have worked on this for a couple hours ( i am obviously new to vb.net so dont laugh please, I am not evn sure how to post my code in here) I missed 2 weeks of class b/c of eye surgery and I am so lost!
Any help would be greately appreiciated!
Thanks
VB Code:
Dim strName, strFirstName, strLastName, strNewName As String
-
Apr 28th, 2004, 01:12 PM
#2
Thread Starter
New Member
my code
Dim mychar As Char = "32"
strName = Me.NameTextbox.Text
strLastName = strName.Substring(Val(char))
strFirstName = strName.Substring(0, Val(char))
Me.NewNameLabel.Text = strLastName & "," & " " & strFirstName
this is just one of the many ways I have tired this
-
Apr 29th, 2004, 11:00 AM
#3
PowerPoster
Hi,
Use the InStr function to find the location of the space.
VB Code:
Dim intLocation As Integer
Dim intLength As Integer
Dim strNameReverse As String
intLength = strName.Length
intLocation = InStr(strName, " ")
strNameReverse = Microsoft.VisualBasic.Right(strname, intLength - intLocation) & ", " & Microsoft.VisualBasic.Left(strname, intLocation - 1)
Edited at 00:48 on 04/30/2004 after checking.
Last edited by taxes; Apr 29th, 2004 at 06:56 PM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Apr 29th, 2004, 11:51 AM
#4
Hyperactive Member
Dear Jenbug, if I understood well, this could be a more general way to solve your problem, expecially useful for names composed by 3, or more, words:
VB Code:
Dim str As String = "Carol Anderson J. Smith"
Dim parts() As String = str.Split(Char.Parse(" "))
str = ""
Dim i As Int16 = parts.Length - 1
Do While i >= 0
str = str & parts(i)
If i >= 1 Then
str = str & ", "
End If
i -= 1
Loop
Debug.WriteLine(str)
In Output window you will read:
Smith, J., Anderson, Carol
Live long and prosper (Mr. Spock)
-
Apr 29th, 2004, 07:49 PM
#5
PowerPoster
Hi Jenbug,
"In Output window you will read:
Smith, J., Anderson, Carol"
Why on Earth would he want to do that?????
For multiple names use
VB Code:
Dim intLocation As Integer
Dim intLength As Integer
Dim strNameReverse(10) As String
Dim strReverse As String
Dim strName As String = "James Michael Smith"
Dim intCount As Integer = 0
Dim intCount1 As Integer = 0
Do While strName.Length > 1
intLength = strName.Length
intLocation = InStr(strName, " ")
If intLocation = 0 Then
strNameReverse(intCount) = strName
Exit Do
End If
strNameReverse(intCount) = Microsoft.VisualBasic.Left(strName, intLocation - 1)
strName = Microsoft.VisualBasic.Right(strName, intLength - intLocation)
intCount = intCount + 1
Loop
strReverse = strNameReverse(intCount) & ","
For intCount1 = 0 To intCount - 1
strReverse = strReverse & " " & strNameReverse(intCount1)
Next
strReverse will now show
"Smith, James Michael"
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Apr 30th, 2004, 06:12 AM
#6
Hyperactive Member
Ehm, ehm,...I premitted:"if I understood well"
Really, perhaps that way to write names is an english habit, we don't use it. I thought our friends Jenbug needed to obtain a particular format to export/import in other application, or....I don't know!
Anyway I tried to adapt my code:
VB Code:
Dim parts() As String = str.Split(Char.Parse(" "))
str = ""
Dim i As Int16 = parts.Length - 1
Do While i >= 0
str = str & parts(i)
If i = parts.Length - 1 Then
str = str & ", "
Else
str = str & " "
End If
i -= 1
Loop
Debug.WriteLine(str.TrimEnd)
and now you can read:
Smith, J. Anderson Carol
Anyway your code is surely good, too. This is a little more compact. I had to write this for a friend and so I had time to squeeze my brain on the problem. I'm a beginner. I'm sure you are a better programmer than me (really think so!) and I'm sure that in 95% of situations your code should be better of mine, too. But in this particular application, I prefer mine. Then Jenbug will decide...! Good job.
Live long and prosper (Mr. Spock)
-
Apr 30th, 2004, 06:30 AM
#7
PowerPoster
Hi alextyx,
Full names are usually presented in Western languages (not only English) in two ways:
1. In forenames, surname order (which is easier on the eye but renders sorting more difficult)
2. Surname first then forenames in natural order. (This enables automatic sorting in the more frequently required manner).
I have assumed that this is what Jenbug requires.
You can still use your approach of the string.split but you will need to hold the results of the split in a manner from which the surname can be isolated.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Apr 30th, 2004, 10:28 PM
#8
Thread Starter
New Member
Thanks so much for your help! I appreicate all of your responses and with some (ok LOTS) of trial and error I finally got it ... I hope that someday I know as much about code as all of you. Seems like there is always several ways to code each problem.
Thanks again 
Jen
-
May 2nd, 2004, 09:32 AM
#9
Hyperactive Member
Ok Taxes. What I have never seen, in our burocratic country (Italy), is the use "Surname", "FirstName" or "Surname", "SecondName" "FirstName".
I've never seen to put a comma among them, in ANY position! But I'm not sure if it's used in some particular situation.
Good programming, to you and to JenBug!
Live long and prosper (Mr. Spock)
-
May 2nd, 2004, 01:50 PM
#10
PowerPoster
Hi,
"What I have never seen, in our burocratic country (Italy), is the use "Surname", "FirstName" "
How on Earth do you list names in your telephone directories???
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 3rd, 2004, 02:43 AM
#11
Hyperactive Member
-
May 4th, 2004, 12:04 PM
#12
PowerPoster
Hi alextyx,
Your English is much better than my Italian
The reason why a comma is used is to indicate the end of the surname. This is necessary where there is a double surname, e.g
Full name is John Michael Smith Jones. If the surname is Smith Jones then is is shown
Smith Jones, John Michael.
whereas if it was shown
Smith Jones John Michael, you would not know if the surname was Smith or Smith Jones.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 5th, 2004, 02:54 AM
#13
Hyperactive Member
It's true, Taxes. I understand the reason. We don't use, but I have to admit that it could be useful. Normally we are able to understand what is the surname, anyway, but some cases could be ambiguos!Bye
Live long and prosper (Mr. Spock)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|