Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split string without delimeter 2

Status
Not open for further replies.

JadeKnight

IS-IT--Management
Feb 23, 2004
94
NO
How can I split a string without a delimeter.
Let's say I've got this string : abcdefghijklmn, and I would like to split it into an array of two characters, how can this be done?

I've been twisting my brain for this a couple of hours, and I can't figure it out. Any help would be appreciated :)
 
You would need to write your own splitter. Look at the Mid function.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Here is one that I just wrote. I haven't tested it extensively, but it should be what you need.:
Code:
Option Explicit

Dim strText
Dim arrResult
Dim i
Dim nStart

strText = "aabbccddeeff"
arrResult = LenSplit(strText, 2)
For i=0 To UBound(arrResult)
	WScript.Echo i & ") " & arrResult(i)
Next
strText = "aaabbbcccdddeeefffggg"
arrResult = LenSplit(strText, 3)
For i=0 To UBound(arrResult)
	WScript.Echo i & ") " & arrResult(i)
Next
strText = "aabbccddeeff"
arrResult = LenSplit(strText, 4)
For i=0 To UBound(arrResult)
	WScript.Echo i & ") " & arrResult(i)
Next
strText = "aabbccddeeff"
arrResult = LenSplit(strText, 5)
For i=0 To UBound(arrResult)
	WScript.Echo i & ") " & arrResult(i)
Next
Function LenSplit(strTxt, nLen)
	Dim j, k
	Dim arr()
	Dim strPart
	
	j = 0
	Do While j*nLen <= Len(strTxt)
		nStart = CInt((j*nLen) + 1)
		strPart = Mid(CStr(strTxt), CInt(nStart), CInt(nLen))
		If strPart <> "" And Not IsNull(strPart) Then
			ReDim Preserve arr(j)
			arr(j) = strPart
		End If
		j = j + 1
	Loop
	LenSplit = arr
End Function

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
This should be a little faster since it's not performing a ReDim Preserve for each element added:

Function LenSplit(strTxt, nLen)
Dim arr()
Dim arrlength, sindex

arrlength = Len(strText) \ nLen

If Len(strText) Mod nLen > 0 Then
arrlength = arrlength + 1
End If

Redim arr(arrlength-1)

For sindex = 0 to arrlength-1
arr(sindex)=Mid(strText, (sindex * nLen) + 1, nLen)
Next

LenSplit = arr
End Function
 
Much better.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thx to both of you. Would have taken me ages to figure it out... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top