String Manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Benniit
    New Member
    • May 2008
    • 54

    #1

    String Manipulation

    I'm using VB 6.0 and I have a text field called FileNo and the value for it e.g. 52281/TJ, 234/TJ, 2345/TJ.
    Now, i want if a user enters the fileNo as 52281 TJ or 52281TJ or 52281 /TJ, it should change it to 52281/TJ , 234/TJ etc. Whether there is a space between the number and the /TJ or only if the TJ is added to the number, it should always add the /TJ to whatever number is entered. Hope is clear.
    Thanx Gurus.

    Ben
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    I think it can be done using INSTR or using REPLACE:

    Check this example using REPLACE (its easier but is not as fast as using INSTR:

    [CODE=vb]dim Str1 as string
    str1=inputbox(" write file number")
    srt1=replace(st r1, " ", "")
    str1=replace(st r1,"/""")
    str1=replace(st r1,"TJ", "/TJ")
    msgbox str1[/CODE]

    Comment

    • leoce7
      New Member
      • Aug 2008
      • 8

      #3
      If you want it a little less hard coded with the option of interpreting different initials ........ here is a crude example of a function you may want to try:

      ' =============== =============== =============== ============
      Public Function StringManipulat ion(str As String)

      Dim str2 As String
      Dim index, i As Integer
      Dim c, cN

      ' trim leading/trailing spaces & make it all uppercase (change UCase to LCase for opposite)
      str = Trim$(UCase(str ))

      ' walk thru the string
      For index = 1 To Len(str)

      ' get a character
      c = Mid(str, index, 1)
      'If c = " " Then c = 0
      'If c = "/" Then c = 47

      Select Case Asc(c)
      ' ASCII code for "/" character
      Case 47
      str2 = str2 & c
      ' ASCII code for numbers
      Case 48 To 89
      str2 = str2 & c
      ' ASCII code for uppercase letters (97 to 122 for lowercase)
      Case 65 To 90
      'If Int(Mid(str2, Len(str2), 1)) = False & Mid(str2, Len(str2), 1) <> "/" Then str2 = str2 & "/"
      str2 = str2 & c
      End Select

      Next index

      ' if the "/" character was included
      If InStr(1, str2, "/") = False Then
      For i = 1 To Len(str2) - 1
      c = Mid(str2, i, 1)
      cN = Mid(str2, i + 1, 1)
      If Val(c) <> 0 & Val(cN) = 0 Then
      str2 = Mid(str2, 1, i - 1) & "/" & Mid(str2, i, Len(str2))
      GoTo Done
      End If
      Next i
      End If

      Done:

      MsgBox str2

      End Function

      ' =============== =============== =============== ============

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Hi,

        OK, Say your Text1 contains the number,
        Write this in LostFocus or Validate event of Text1

        Text1.Text = Val(Text1.Text) & "/TJ"

        Regards
        Veena

        Comment

        Working...