Results 1 to 13 of 13

Thread: String Manipulation ( to flip names in textbox)

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    7

    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:
    1. Dim strName, strFirstName, strLastName, strNewName As String

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    7

    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

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Use the InStr function to find the location of the space.

    VB Code:
    1. Dim intLocation As Integer
    2.         Dim intLength As Integer
    3.         Dim strNameReverse As String
    4.         intLength = strName.Length
    5.         intLocation = InStr(strName, " ")
    6.         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.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    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:
    1. Dim str As String = "Carol Anderson J. Smith"
    2.  
    3.         Dim parts() As String = str.Split(Char.Parse(" "))
    4.         str = ""
    5.         Dim i As Int16 = parts.Length - 1
    6.         Do While i >= 0
    7.             str = str & parts(i)
    8.             If i >= 1 Then
    9.                 str = str & ", "
    10.             End If
    11.             i -= 1
    12.         Loop
    13.  
    14.         Debug.WriteLine(str)

    In Output window you will read:
    Smith, J., Anderson, Carol
    Live long and prosper (Mr. Spock)

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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:
    1. Dim intLocation As Integer
    2.         Dim intLength As Integer
    3.         Dim strNameReverse(10) As String
    4.         Dim strReverse As String
    5.         Dim strName As String = "James Michael Smith"
    6.         Dim intCount As Integer = 0
    7.         Dim intCount1 As Integer = 0
    8.         Do While strName.Length > 1
    9.  
    10.             intLength = strName.Length
    11.             intLocation = InStr(strName, " ")
    12.             If intLocation = 0 Then
    13.                 strNameReverse(intCount) = strName
    14.                 Exit Do
    15.             End If
    16.             strNameReverse(intCount) = Microsoft.VisualBasic.Left(strName, intLocation - 1)
    17.             strName = Microsoft.VisualBasic.Right(strName, intLength - intLocation)
    18.             intCount = intCount + 1
    19.         Loop
    20.         strReverse = strNameReverse(intCount) & ","
    21.         For intCount1 = 0 To intCount - 1
    22.             strReverse = strReverse & " " & strNameReverse(intCount1)
    23.         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.

  6. #6
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    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:
    1. Dim parts() As String = str.Split(Char.Parse(" "))
    2.         str = ""
    3.         Dim i As Int16 = parts.Length - 1
    4.         Do While i >= 0
    5.             str = str & parts(i)
    6.             If i = parts.Length - 1 Then
    7.                 str = str & ", "
    8.             Else
    9.                 str = str & " "
    10.             End If
    11.             i -= 1
    12.         Loop
    13.         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)

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    7
    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

  9. #9
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    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)

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  11. #11
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    My God! My English is really poor !
    What I want to explain is that we don't use comma (",") but only blank spaces to separate surname from name.

    Example of your way:

    Smith, K. John

    Example of our way:

    Smith K. John

    The only difference is in use of comma.
    That's why I didn't catch exactly what Jenbug needed!
    Bye taxes, have a nice day
    Live long and prosper (Mr. Spock)

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  13. #13
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    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
  •  



Click Here to Expand Forum to Full Width