extract a substring from string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • charvi
    New Member
    • May 2007
    • 121

    extract a substring from string

    i have a name as John smith which i enter in textbox now i want to extract John from John Smith how to do
    Thanx in advance
  • DGee
    New Member
    • Feb 2008
    • 5

    #2
    Well it depends whether you know what,where and how long the string is that you want to extract. If you do then i would use Mid(), ie the string "John Smith" = 10 characters. So (Where ever extracted to) = Mid((string or "John Smith"), (1 as "John" starts at position 1), (4 as the length of John = 4). So ... = Mid(string,1,4) .


    Alternativly if you had a load of strings and you needed to get the first name from all of them you could use:

    data = currentname
    x=1

    Do
    If Mid(data, X, 1) = " " Then
    ' end of first name
    Else
    ' include current symbol with retrieved first name
    data2 = data2 & Mid(data, X, 1)
    X = X + 1
    End If
    Loop Until Mid(data, X, 1) = " "
    'retrieved data = data2

    Comment

    • creative1
      Contributor
      • Sep 2007
      • 274

      #3
      I think split() is a good choice for this

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by creative1
        I think split() is a good choice for this
        Yes, you can build the logic to do pretty much whatever you want along these lines, using loops, Mid() function, and depending on your VB version, also Left() and Right() functions. But if the situation is suited to it, the Split() function will be much quicker and simpler.

        Comment

        Working...