Results 1 to 5 of 5

Thread: Parsing string- insert characters

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    152

    Parsing string- insert characters

    I have a string as follows:

    041204350431002D043004340440

    What I would like to do is after every 4 characters, insert some characters. At the beginning I want a < and at the end a >. I am getting close but I always get:

    0412--0435--0431--002D--0430--0434--0440--

    This has to be simple and it may just be one of those days, but does anyone have a quick answer? I just can't figure out how not to add it to the end and instead add the >

    Thanks.

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Hi ther Imanta, don't know exactly what you are after, but I think this is close to what you want

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim i As Integer
    3.     Dim s As String
    4.     Dim sres As String
    5.     Dim sSplitString As String
    6.     sSplitString = "--"
    7.     s = "041204350431002D043004340440"
    8.     sres = "<"
    9.     For i = 1 To Len(s)
    10.         If (i - 1) Mod 4 = 0 And i <> 1 Then
    11.             sres = sres & sSplitString & Mid(s, i, 1)
    12.         Else
    13.             sres = sres & Mid(s, i, 1)
    14.         End If
    15.     Next i
    16.     sres = sres & ">"
    17.     MsgBox sres
    18. End Sub
    -= a peet post =-

  3. #3
    Hyperactive Member Q_Me's Avatar
    Join Date
    Dec 2001
    Posts
    327
    Try this:
    VB Code:
    1. 'After your function of formatting:
    2.  
    3. Private Function KillExtra(YourPreMadeString, KillLength As Integer) As String 'Insert Length for the Extra "-" or "--" ect.
    4. Dim Orgnl as String
    5. Orgnl = YourPreMadeString
    6. If Right(Orgnl, KillLength) = "--" Then
    7.      NewStrng$=Left(Orgnl, Len(Orgnl - KillLength))
    8.      KillExtra = NewString$
    9.      'This will kill the extra "--"
    10.      'Also this code will work with any length you want
    11. End If
    12. End Sub
    53323737 15 743 313402 05 740313063. 17 15 4150 743 313402 05 140393403437 5203 743 30210.


  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    a better way than my first sample might be to use step...

    something like this

    VB Code:
    1. Private Sub Command2_Click()
    2.     Dim i As Integer
    3.     Dim s As String
    4.     Dim sres As String
    5.     Dim sSplitString As String
    6.     Dim iStep As Integer
    7.     iStep = 4
    8.     sSplitString = "--"
    9.     s = "041204350431002D043004340440"
    10.     sres = "<" & Left(s, 4)
    11.     For i = iStep + 1 To Len(s) Step iStep
    12.         sres = sres & sSplitString & Mid(s, i, iStep)
    13.     Next i
    14.     sres = sres & ">"
    15.     Debug.Print sres
    16. End Sub
    -= a peet post =-

  5. #5
    Lively Member
    Join Date
    Oct 2001
    Location
    Florida
    Posts
    98
    Kind of a long way of doing it, but should work easily enough for any length. Although there are a thousand ways to do this.

    Did this really quick like so could probably use some refining.

    VB Code:
    1. Private Sub Command1_Click()
    2.    
    3.     Dim sOriginal As String     'the original string with no deliminating characters.
    4.     Dim sNew As String          'the new string all seperated by deliminating chars.
    5.     Dim sDeliminator As String  'the character we will insert between each 4 digitis.
    6.     Dim sBookEnds As String     'The book end char so to speak. added to beginning and end of sNew
    7.     Dim intLength As Integer    'Integer length of the original string.
    8.    
    9.     'Assign variables needed. Can be obtained via other means. Text boxes etc.
    10.     sOriginal = "041204350431002D043004340440"
    11.     sDeliminator = "--"
    12.     sBookEnds = "<>"
    13.    
    14.     Do Until Len(sOriginal) = 0
    15.        
    16.         'Stores the length of the original string
    17.         intLength = Len(sOriginal)
    18.        
    19.         'If the length of the string is greater than 4
    20.         '   add 4 chars to the new string and delete the first 4 from the original.
    21.         'if the length is 4 or less (last pass through)
    22.         '   No need for deliminator last pass, so add original to new and done.
    23.         If intLength > 4 Then
    24.             'Builds the new string with the left most 4 characters
    25.             'and adds the deliminator.
    26.             sNew = sNew & Left(sOriginal, 4) & sDeliminator
    27.             'removes the left most 4 characters from the string.
    28.             sOriginal = Right(sOriginal, intLength - 4)
    29.         ElseIf intLength > 0 And intLength <= 4 Then
    30.             'Adds the last 1 to 4 characters to the string.
    31.             'No need for a deliminator since it should already have one.
    32.             sNew = sNew & sOriginal
    33.             'Clears the rest of the original string since we just added
    34.             'the rest of it to the new string.
    35.             sOriginal = ""
    36.         End If
    37.        
    38.     Loop
    39.    
    40.     'OK done creating the string lets throw the bookends on.
    41.     'If statement just checks if the bookends are two characters or one
    42.     'If two characters puts the first one on first and last on second.
    43.     'If one character puts it on each end.
    44.     If Len(sDeliminator) = 1 Then
    45.         sNew = sBookEnds & sNew & sBookEnds
    46.     ElseIf Len(sDeliminator) = 2 Then
    47.         sNew = Left(sBookEnds, 1) & sNew & Right(sBookEnds, 1)
    48.     End If
    49.    
    50.     MsgBox sNew
    51.    
    52. End Sub

    Create a form and put a command button in it and copy this code for example.

    EDIT: HEHEHE gotta love these boards, as i mentioned 3 other methods above this one already in like 20 minutes

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