Results 1 to 9 of 9

Thread: Splitting Up Words

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Splitting Up Words

    How would i do this.

    my names keep getting returned as

    StephenBrown
    SimonSmith
    NickScott

    and i want them to be

    Stephen Brown
    Simon Smith
    Nick Scott

    so how would i make VB but a space b4 the second capped letter ?
    Im Learning !!!!

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Splitting Up Words

    How are you gettin those names in the first place?

    I ask, because you may be able to add a space whilst concatenating (joining) the first and last name

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Splitting Up Words

    sorry probably wasnt clear the program to get the names it coded by me. Someone has just asked me to make a prog to split the names up.

    But i didn't know where to start of if its even possible.
    Im Learning !!!!

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Splitting Up Words

    This separates all subnames (even more than 2) that start with upper case:

    VB Code:
    1. Private Function GetName(pName As String) As String
    2. Dim i     As Integer
    3. Dim lName As String
    4.    
    5.     For i = 2 To Len(pName)
    6.         If Mid$(pName, i, 1) = UCase(Mid$(pName, i, 1)) Then
    7.             pName = Left$(pName, i - 1) & " " & Right$(pName, Len(pName) + 1 - i)
    8.             i = i + 1
    9.         End If
    10.     Next
    11.     GetName = pName
    12. End Function

    Anyhow, it would do things much slower, as Bruce said, it would be better separate the fields before.
    Last edited by jcis; Nov 13th, 2005 at 08:42 PM.

  5. #5
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: Splitting Up Words

    Using the constants I found on MSDN and I used collections to parse out the pieces of a passed in name -

    It's not totally fool-proof (J. R. R. Tolkien, for example will not parse correctly, but other common name combinations will. ie: Mr & mrs john m smith; Joe Smith; Joe W. Smith, Jr; etc)




    VB Code:
    1. Option Explicit
    2.  
    3.  
    4. Const Pedigrees = "Jr.Sr.III,IV,VIII,IX,XIII"
    5.  
    6. ' dim these so the ParseIt method will return by reference the values
    7. Dim mtitle As String, ftitle As String, sfirstname As String
    8. Dim smi As String, slastname As String, ssuffix As String
    9.  
    10. Sub Command1_Click()
    11.  
    12. ' txtName textbox for testing
    13. ParseIt txtName, mtitle, ftitle, sfirstname, smi, slastname, ssuffix
    14.  
    15.  
    16. ' show results
    17. Debug.Print mtitle
    18. Debug.Print ftitle
    19. Debug.Print sfirstname
    20. Debug.Print smi
    21. Debug.Print slastname
    22. Debug.Print ssuffix
    23.  
    24.  
    25. End Sub
    26. Private Function ParseIt(s As String, mtitle As String, ftitle As String, sfirstname As String, _
    27.     smi As String, slastname As String, ssuffix As String)
    28.  
    29. Dim mArray As Variant
    30. Dim fArray As Variant
    31. Dim v As Variant
    32. Dim bmTitleFound As Boolean
    33. Dim fmTitleFound As Boolean
    34. Dim b2in1 As Boolean
    35. Dim ix As Long
    36. Dim ij As Long
    37.  
    38. Dim lCount As Long
    39.  
    40.  
    41. ' default values
    42. mtitle = vbNullString
    43. ftitle = vbNullString
    44. sfirstname = vbNullString
    45. smi = vbNullString
    46. slastname = vbNullString
    47. ssuffix = vbNullString
    48.  
    49.  
    50. ' split name into an array
    51. v = Split(s, " ")
    52.  
    53. If IsEmpty(v) Then Exit Function
    54.  
    55. Dim mcol As Collection
    56. Set mcol = New Collection
    57.  
    58. If IsArray(v) Then
    59.     ' load into a collection
    60.     For ix = LBound(v) To UBound(v)
    61.         If v(ix) <> "" Then
    62.             mcol.Add v(ix), CStr(ix)
    63.         End If
    64.     Next
    65. End If
    66.  
    67. ' build array
    68. mArray = Array("Mr", "Mr.")
    69. fArray = Array("Mrs", "Mrs.", "Miss", "Ms", "Ms.")
    70.  
    71. ' check for a title
    72. 'For ix = 1 To mcol.Count
    73. lCount = mcol.Count
    74. If lCount > 0 Then
    75.     For ix = 1 To lCount 'mcol.Count
    76.        
    77.         If ix > lCount Then Exit For
    78.        
    79.         If Not bmTitleFound Then
    80.         For ij = LBound(mArray) To UBound(mArray)
    81.             If StrComp(mcol.Item(ix), mArray(ij), vbTextCompare) = 0 Then
    82.                 mtitle = mcol.Item(ix)
    83.                 mcol.Remove ix
    84.                 bmTitleFound = True
    85.                 lCount = lCount - 1
    86.                 Exit For
    87.             End If
    88.         Next
    89.         End If
    90.        
    91.         If Not fmTitleFound Then
    92.         For ij = LBound(fArray) To UBound(fArray)
    93.             If StrComp(mcol.Item(ix), fArray(ij), vbTextCompare) = 0 Then
    94.                 ftitle = mcol.Item(ix)
    95.                 mcol.Remove ix
    96.                 fmTitleFound = True
    97.                 lCount = lCount - 1
    98.                 Exit For
    99.             End If
    100.         Next
    101.         End If
    102.     Next
    103.    
    104.     ' remove ampersand if exists
    105.     If mcol.Count > 0 Then
    106.         For ix = 1 To mcol.Count
    107.             If InStr(mcol.Item(ix), "&") > 0 Then
    108.                 mcol.Remove ix
    109.                 b2in1 = True
    110.                 Exit For
    111.             End If
    112.         Next
    113.     End If
    114.    
    115.     ' get first name
    116.     If mcol.Count > 0 Then
    117.         sfirstname = mcol.Item(1): mcol.Remove 1
    118.     End If
    119.    
    120.     ' get suffix
    121.     If mcol.Count > 0 Then
    122.         For ix = 1 To mcol.Count
    123.             If InStr(1, Pedigrees, mcol.Item(ix), vbTextCompare) > 0 Then
    124.                 ssuffix = mcol.Item(ix)
    125.                 mcol.Remove ix
    126.                 Exit For
    127.             End If
    128.         Next
    129.     End If
    130.    
    131.     ' get last name
    132.     If mcol.Count > 0 Then
    133.         slastname = mcol.Item(mcol.Count): mcol.Remove mcol.Count
    134.         slastname = Replace(slastname, ",", "")
    135.     End If
    136.    
    137.     ' check for middle initial/name
    138.     If mcol.Count > 0 Then
    139.         smi = mcol.Item(1)
    140.     End If
    141. End If
    142.  
    143. End Function

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Splitting Up Words

    ZeBula8,

    Unfortunatly I don't think that will work as is. The passed Name (s) dosn't have a space (the deliniator your using with the Split()), so the function will fall thru each time.

  7. #7
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: Splitting Up Words

    Right, Bruce -

    I totally misread the original question -

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Splitting Up Words

    I'm sure it will still come in handy

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Splitting Up Words

    Names are very hard to split, as they can have too many variations. best to have 4 different fields. That way you know if the first name has a space in it, and the last name has 2 spaces, and then have a jr/sr suffix. Your split could have up to 10 indexes.

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