PDA

View Full Version : Split string into multiple parts no bigger than 240 chars?



PsYiOn
06-20-2008, 09:07 AM
I need to split a string into 8 parts that will be at most 255 chars. so i end up with string1 string2 and string3 etc

Can anyone point me in the right direction? possible an article or some sample code?


Regards

PsyIon

figment
06-20-2008, 09:52 AM
you should be able to do this with the Left(), Right(), or Mid() commands

i would use Mid() and Right() in something like this

Sub samp()
Dim strstart As String, strresult(1 To 8) As string, a As Long
strstart= 'what ever your starting string is
For a = 1 To 8
If a + ((a - 1) * 255) + 254 < Len(strstar) Then
strresult(a) = Mid(strstar, a + ((a - 1) * 255), 255)
Else
stresult(a) = Right(strstar, Len(strstar) - (a + ((a - 1) * 255)))
Exit For
End If
Next
End Sub

RonMcK
06-20-2008, 10:12 AM
Sub samp()
Dim strstart As String, strresult(1 To 8) As sting, a As Long
<snip>
End Sub

Sting? Prehaps, string?

Cheers,

figment
06-20-2008, 10:19 AM
good catch thanks

RonMcK
06-20-2008, 10:22 AM
PsYiOn,

I used a slightly different approach, slicing your string into 8 more or else equal length pieces.Sub split_string()
Dim bigstr As String
Dim str(1 To 8) As String
Dim LenStr As Long
Dim LenPtStr As Long
Dim FracPt As Long
Dim factor As Long

bigstr = "abcdefghijklmopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" _
& "abcdefghijklmopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

LenStr = Len(bigstr)
If LenStr < 8 Then GoTo Macro_done
If LenStr > 8 * 255 Then GoTo Macro_done
LenPtStr = Int(LenStr / 8)
FracPt = LenStr Mod 8
Debug.Print LenStr, LenPtStr, FracPt
For I = 1 To 8
If I < 8 Then factor = LenPtStr Else factor = LenPtStr + FracPt
str(I) = Mid(bigstr, 1 + (I - 1) * LenPtStr, factor)
Debug.Print str(I), Len(str(I))
Next I

Macro_done:
End Sub


Here's the debug output:

122 15 2
abcdefghijklmop 15
qrstuvwxyz01234 15
56789ABCDEFGHIJ 15
KLMNOPQRSTUVWXY 15
Zabcdefghijklmo 15
pqrstuvwxyz0123 15
456789ABCDEFGHI 15
JKLMNOPQRSTUVWXYZ 17

HTH,

RonMcK
06-20-2008, 10:24 AM
Figment,

You're welcome!

Have a good day,

PsYiOn
06-20-2008, 10:54 AM
PsYiOn,

I used a slightly different approach, slicing your string into 8 more or else equal length pieces.Sub split_string()
Dim bigstr As String
Dim str(1 To 8) As String
Dim LenStr As Long
Dim LenPtStr As Long
Dim FracPt As Long
Dim factor As Long

bigstr = "abcdefghijklmopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" _
& "abcdefghijklmopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

LenStr = Len(bigstr)
If LenStr < 8 Then GoTo Macro_done
If LenStr > 8 * 255 Then GoTo Macro_done
LenPtStr = Int(LenStr / 8)
FracPt = LenStr Mod 8
Debug.Print LenStr, LenPtStr, FracPt
For I = 1 To 8
If I < 8 Then factor = LenPtStr Else factor = LenPtStr + FracPt
str(I) = Mid(bigstr, 1 + (I - 1) * LenPtStr, factor)
Debug.Print str(I), Len(str(I))
Next I

Macro_done:
End Sub

Here's the debug output:

122 15 2
abcdefghijklmop 15
qrstuvwxyz01234 15
56789ABCDEFGHIJ 15
KLMNOPQRSTUVWXY 15
Zabcdefghijklmo 15
pqrstuvwxyz0123 15
456789ABCDEFGHI 15
JKLMNOPQRSTUVWXYZ 17

HTH,

This looks perfect but im a bit confused as to how to use the str() to set a seperate string for each eg string1 string2 etc etc
[code]
string1 = str(1)
string2 - str(2)
[code]
would i do it like that?

Ragards

PsYiOn

figment
06-20-2008, 11:00 AM
[code]
string1 = str(1)
string2 = str(2)
[code]

that is correct.

RonMcK
06-20-2008, 11:01 AM
This looks perfect but im a bit confused as to how to use the str() to set a seperate string for each eg string1 string2 etc etc
[code]
string1 = str(1)
string2 - str(2)
[code]
would i do it like that?


Yes.

RonMcK
06-20-2008, 11:16 AM
PsYiOn,

Depending on who will be using the worksheet this code is going to be in, you may want to provide (buildd into the code) some default reponses when the string is too short (less than 8 chars or whatever you deem to be too short) or too long (greater than 255 * 8 (1,440)). Is there a need to preserve the excess or can you just truncate?

Cheers,