n characters as string split delimiter?

B

Brent Bigler

You can use a character as a delimiter to split a string (e.g.,
Split(myString, ","), but can you split a string using a given number
of characters? Say you have a fixed-width text file where any given
record occupies three rows. The total number of characters per record
is 1086 (3 lines X 160 characters per line + 6 characters for each
vbCRLF). What sort of code could you use to split up the text file?
(pseudocode: split(textFileArray, 1086))

Thanks for any help!

--Brent
 
C

Cor

Hi Brent,

Very dirty written here in VB a little bit pseudo
\\\
dim myarray() as string
dim indexer as integer = 0
dim i as integer
For i = 0 to record.length - 7
myarray(indexer) = record.substring(i, 6)
indexer += 1
next
myarray(indexer) = record.substring(i, record.length-i)
////

I did not test it at all, just typed here but I think it should do it?

Cor
 
J

Jon Skeet [C# MVP]

Check out Regex.Split method.

I would have thought using a Regex would be overkill, myself. I'd just
go with something like:

string[] SplitByLength (string data, int length)
{
string[] ret = new string[(data.Length+length-1)/length];

for (int i=0; i < ret.Length-1; i++)
{
ret = data.Substring(i*length, length);
}
ret [ret.Length-1] = data.Substring ((ret.Length-1)*length);
return ret;
}

That will (I think - it's untested) give a load of strings of exactly
length "length", and the final string may be shorter than "length". It
would be easy to change it to either ignore any extra bits, or throw an
exception if the original data wasn't an exact multiple of length.

I'd expect that to be considerably faster than a Regex for large
strings.
 
C

Cor

Hi Jon,

Almost exactly as mine, (Altough I forgot to set the array lenght and did
not make it generic)

I do not believe you can type that fast to copy it, so this is really nice
that it is the same.

I was thinking if i would make it in C# also but thought why if the OP does
not tell this and now it is there.

Cor
 
C

Cor

Hi Brent,

Looking at the code from Jon, I saw some errors in my code sample, I did
give to you.

I corrected them (I hope completly), the approach is a very little bit else
than Jon, the idea is the same. (I also did make it generic like he did).

Cor
\\\
Private Function splitonlength(ByVal record As String, _
ByVal size As Integer) As String()
Dim myarray(CInt(record.Length / size)) As String
Dim indexer As Integer = 0
Dim i As Integer
For i = 0 To record.Length - size - 1 Step size
myarray(indexer) = record.Substring(i, size)
indexer += 1
Next
myarray(indexer) = record.Substring(i, record.Length - i)
Return myarray
End Function
///
 
J

Jay B. Harlow [MVP - Outlook]

Brent,
As Cor & Jon pointed out you can use the String.SubString function to get
the first 1086 characters of a string. Alternatively you could use the Left,
Right, and Mid functions in the Microsoft.VisualBasic.Strings module. Just
remember that String.SubString accepts base 0 indexes, while
Microsoft.VisualBasic.Strings accept base 1 indexes.

Otherwise there are three Split functions in .NET:

Use Microsoft.VisualBasic.Strings.Split if you need to split a string based
on a specific word (string). It is the Split function from VB6.

Use System.String.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

Use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top