|
-
Sep 24th, 2002, 02:04 PM
#1
Thread Starter
Addicted Member
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.
-
Sep 24th, 2002, 02:39 PM
#2
-= B u g S l a y e r =-
Hi ther Imanta, don't know exactly what you are after, but I think this is close to what you want 
VB Code:
Private Sub Command1_Click()
Dim i As Integer
Dim s As String
Dim sres As String
Dim sSplitString As String
sSplitString = "--"
s = "041204350431002D043004340440"
sres = "<"
For i = 1 To Len(s)
If (i - 1) Mod 4 = 0 And i <> 1 Then
sres = sres & sSplitString & Mid(s, i, 1)
Else
sres = sres & Mid(s, i, 1)
End If
Next i
sres = sres & ">"
MsgBox sres
End Sub
-
Sep 24th, 2002, 02:41 PM
#3
Hyperactive Member
Try this:
VB Code:
'After your function of formatting:
Private Function KillExtra(YourPreMadeString, KillLength As Integer) As String 'Insert Length for the Extra "-" or "--" ect.
Dim Orgnl as String
Orgnl = YourPreMadeString
If Right(Orgnl, KillLength) = "--" Then
NewStrng$=Left(Orgnl, Len(Orgnl - KillLength))
KillExtra = NewString$
'This will kill the extra "--"
'Also this code will work with any length you want
End If
End Sub
53323737 15 743 313402 05 740313063. 17 15 4150 743 313402 05 140393403437 5203 743 30210.

-
Sep 24th, 2002, 02:48 PM
#4
-= B u g S l a y e r =-
a better way than my first sample might be to use step...
something like this
VB Code:
Private Sub Command2_Click()
Dim i As Integer
Dim s As String
Dim sres As String
Dim sSplitString As String
Dim iStep As Integer
iStep = 4
sSplitString = "--"
s = "041204350431002D043004340440"
sres = "<" & Left(s, 4)
For i = iStep + 1 To Len(s) Step iStep
sres = sres & sSplitString & Mid(s, i, iStep)
Next i
sres = sres & ">"
Debug.Print sres
End Sub
-
Sep 24th, 2002, 03:03 PM
#5
Lively Member
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:
Private Sub Command1_Click()
Dim sOriginal As String 'the original string with no deliminating characters.
Dim sNew As String 'the new string all seperated by deliminating chars.
Dim sDeliminator As String 'the character we will insert between each 4 digitis.
Dim sBookEnds As String 'The book end char so to speak. added to beginning and end of sNew
Dim intLength As Integer 'Integer length of the original string.
'Assign variables needed. Can be obtained via other means. Text boxes etc.
sOriginal = "041204350431002D043004340440"
sDeliminator = "--"
sBookEnds = "<>"
Do Until Len(sOriginal) = 0
'Stores the length of the original string
intLength = Len(sOriginal)
'If the length of the string is greater than 4
' add 4 chars to the new string and delete the first 4 from the original.
'if the length is 4 or less (last pass through)
' No need for deliminator last pass, so add original to new and done.
If intLength > 4 Then
'Builds the new string with the left most 4 characters
'and adds the deliminator.
sNew = sNew & Left(sOriginal, 4) & sDeliminator
'removes the left most 4 characters from the string.
sOriginal = Right(sOriginal, intLength - 4)
ElseIf intLength > 0 And intLength <= 4 Then
'Adds the last 1 to 4 characters to the string.
'No need for a deliminator since it should already have one.
sNew = sNew & sOriginal
'Clears the rest of the original string since we just added
'the rest of it to the new string.
sOriginal = ""
End If
Loop
'OK done creating the string lets throw the bookends on.
'If statement just checks if the bookends are two characters or one
'If two characters puts the first one on first and last on second.
'If one character puts it on each end.
If Len(sDeliminator) = 1 Then
sNew = sBookEnds & sNew & sBookEnds
ElseIf Len(sDeliminator) = 2 Then
sNew = Left(sBookEnds, 1) & sNew & Right(sBookEnds, 1)
End If
MsgBox sNew
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|