Re: chop, separate, split a STRING into sections?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Terry Moreau wrote:
seems like a rather common and simple task for .NET, but I can't seem
to find any "elegant" way to do it without using MID or SUBSTRING and
code looping such as DO WHILE or FOR NEXT.

Here's a sample of what I'm looking for: break apart a string into
sections of 4 characters each...

DIM strMyInputString as String = "01234567ABCDEFGHIJ"
DIM strMyOutputString() as String = {some kind sectioning function or
method that operates on strMyInputString}

and here's the desired output string array:

(0) = 0123
(1) = 4567
(2) = ABCD
(3) = EFGH
(4) = IJ

String.Spilt wont' do it cause there's no delimiter
Regex has a Split that from what I can tell does nothing more
than .NET built in Split
Something in Regex that I'm missing maybe?
Select ? well maybe it could be used but I can't find a good VB.NET
example of how to use it !!!

Such a simple task, and there's no 'single function' or elegant 'one-
liner' that can chop an a string into regular sized sections???

What about converting the string to a Character Array then using Group
or Aggregate?
well i can't find good examples of these either so here I am posting
for ideas!

Anything can be a one-liner. ;)

Here's a one-liner that splits the string into an array, and it even manages to get the last two characters in a string too. :)

Dim strMyOutputString() As String = (From m In Regex.Matches(strMyInputString, "[\W\w]{1,4}") Select DirectCast(m, Match).Value).ToArray

It could have been more elegant if only the MatchCollection class would have implemented IEnumerable<Match> and not only IEnumerable.

--
Göran Andersson
_____
http://www.guffa.com
.



Relevant Pages

  • comparing elements of arrays
    ... There has got to be a better way (or more elegant way) to do this. ... I need to log and add any entry in db.cust.com.new that is not in db.cust.com. ... Rotates thru db.cust.com.new and splits each element into a new array then determine if the 1st element of that arrays ... chomp $string; ...
    (perl.beginners)
  • Re: Looking for a slick way to classify relationship between two numbers, without tons of if/else
    ... Before one can "optimize" a function, one needs to get the function correct. ... And if a is positive and b is None, it'll return None, rather than any string. ... That brings us to the question of what is more elegant. ... To me the elegant code is readable first, and efficient second. ...
    (comp.lang.python)
  • Re: chop, separate, split a STRING into sections?
    ... code looping such as DO WHILE or FOR NEXT. ... Public Function Chunk(ByVal str As String, ByVal chunkSize As Integer) As String ... an "elegant" solution. ... While first off, I don't see the problem with mid, substring, or looping:) ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Avoiding duplicate lines?
    ... Not elegant but effective - ... Put the string into an array only after failing a .binarysearch for the string value. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: chop, separate, split a STRING into sections?
    ... Here's a sample of what I'm looking for: break apart a string into ... and there's no 'single function' or elegant 'one- ... Public Function Chop(ByVal sIn As String, ByVal iCars As Integer) As List(Of ... Dim los As New List ...
    (microsoft.public.dotnet.languages.vb)