code for cutting strings (1 Viewer)

dgoulston

Hasn't Got A Clue
Local time
Today, 02:51
Joined
Jun 10, 2002
Messages
403
all i want to do is seperate a string into bits of 4 characters. eg.

dim stringa as string
dim bita as string

stringa = "231832738127312"

while (stringa length) > 0 then
copy first 4 chars of stringa into bita
delete first 4 chars of stringa
write bita to table (i know how to do that bit)
end



basically all i need to know is how to read a string length and how to copy jus the first 4 characters into another varible and then delete them 4 characters from the string.


anyone help?
thanks in advance
DAL
 

Travis

Registered User.
Local time
Yesterday, 18:51
Joined
Dec 17, 1999
Messages
1,332
dim stringa as string
dim bita as string

stringa = "231832738127312"

Do While Len(stringa) > 0 then
bita=left$(stringa,4) 'Gets the Left 4 Characters
stringa=mid$(stringa,5) 'Start at the 5th Character goes to end of String
'write bita to table (i know how to do that bit)
Loop
 

antomack

Registered User.
Local time
Today, 02:51
Joined
Jan 31, 2002
Messages
215
The following will break a string into strings of n characters long. I have left it to yourself to write the code to populate your table since you say you have this worked out.

If the original string is not fully divisible into strings of n characters then the last string will be of less than n characters length.

To test the function try the following
?stringsofnchar("1234567890123456789012",4)
This will result in
1234
5678
9012
3456
7890
12

Code:
Function StringsofnChar(strFull As String, intRequiredStrLen As Integer)

  Dim strStart As String
  Dim strFirstn As String

  If intRequiredStrLen > 0 Then
    strStart = strFull
    While Len(strStart) > 0
      If Len(strStart) > intRequiredStrLen Then
        strFirstn = left(strStart, intRequiredStrLen)
        strStart = Mid(strStart, intRequiredStrLen + 1, Len(strStart) - intRequiredStrLen)
      Else
        strFirstn = left(strStart, Len(strStart))
        strStart = ""
      End If
      ' write strFirstn to the table
      ' enter your code here that you are using to write the new strings to the table
      Debug.Print strFirstn  ' remove this line, just used to print results to debug window as test
    Wend
  Else
    MsgBox "The Required String Length has to be greater than zero.", vbOKOnly, "Strings of n characters"
  End If
  
End Function
 

dgoulston

Hasn't Got A Clue
Local time
Today, 02:51
Joined
Jun 10, 2002
Messages
403
thanx, i did find a way anyways.. jus got back online to say.

thanks for ur help

DAL
 

Users who are viewing this thread

Top Bottom