Re: chop, separate, split a STRING into sections?
- From: Göran Andersson <guffa@xxxxxxxxx>
- Date: Thu, 04 Dec 2008 15:39:22 +0100
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
.
- Follow-Ups:
- References:
- chop, separate, split a STRING into sections?
- From: Terry Moreau
- chop, separate, split a STRING into sections?
- Prev by Date: Re: chop, separate, split a STRING into sections?
- Next by Date: Re: chop, separate, split a STRING into sections?
- Previous by thread: Re: chop, separate, split a STRING into sections?
- Next by thread: From ... In ... Select ? (was Re: chop, separate, split a STRING into sections?)
- Index(es):
Relevant Pages
|