How to only get char from string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaif
    New Member
    • Mar 2008
    • 24

    How to only get char from string

    Hi i have problem with separating Character from string,

    e.g:
    dim S as string
    dim k as string

    S="23 * 12 UBV 100";

    how to i get output k="UBV"

    Its mean only char set if together then i want to put in other variable,

    any idea how to I start this.... Thx very much
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    Try this if the String Length is fixed..:
    k = Mid("23 * 12 UBV 100",9,3)

    Regards
    Veena

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      Originally posted by shaif
      Its mean only char set if together then i want to put in other variable,
      If you want only characters then find out all the digits and replace them with space and finally remove space but not the intermediate spaces.

      Comment

      • shaif
        New Member
        • Mar 2008
        • 24

        #4
        Originally posted by QVeen72
        Hi,

        Try this if the String Length is fixed..:
        k = Mid("23 * 12 UBV 100",9,3)

        Regards
        Veena
        Hi Veena,

        Your idea is ok, however, Value of S can be change. I mean I dont know the exactvalue of S, but I know it is including with digit and character, so i want to seperate only characters, Thx again

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          The solution in post #3 should do the job. For example...

          [CODE=vb]
          Public Function CharsOnly_V1(By Val S As String) As String
          Dim I As Long
          For I = 0 To 9
          S = Replace(S, Format$(I), "")
          Next
          CharsOnly_V1 = Trim$(S)
          End Function
          [/CODE]Demo...
          Print charsonly_v1(" db dfgjlh 35jkl sekjh 54 4")
          db dfgjlh jkl sekjh

          Here's another alternative which has the advantage of allowing you more control over which characters are copied and which aren't.

          [CODE=vb]Public Function CharsOnly_V2(By Val S As String) As String
          Dim I As Long, Char As String * 1
          For I = 1 To Len(S)
          Char = Mid$(S, I, 1)
          Select Case Char
          Case "0" To "9"
          ' Don't touch.
          Case "a" To "z"
          CharsOnly_V2 = CharsOnly_V2 & Char
          Case "A" To "Z"
          CharsOnly_V2 = CharsOnly_V2 & Char
          Case Else
          ' Don't touch.
          End Select
          Next
          End Function
          [/CODE]Demo...

          Print charsonly_v2(" db dfgjlh 35jkl sekjh 54 4")
          dbdfgjlhjklsekj h

          Comment

          Working...