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

Tech-Archive recommends: Fix windows errors by optimizing your registry



On 2008-12-04, Teme64 <teemuhh@xxxxxxxxx> wrote:
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.

Tom Shelton wrote:
<Extension()> _
Public Function Chunk(ByVal str As String, ByVal chunkSize As Integer) As String()
Dim los As New List(Of String)

Dim i As Integer = 1
Dim temp As String

While True
temp = Mid(str, i, chunkSize)
If temp = String.Empty Then Exit While
los.Add(temp)
i += chunkSize
End While

Return los.ToArray()
End Function

That function still uses Mid and While loop. Terry Moreau asked for
an "elegant" solution. Does calling the function to an extension method
make it an "elegant" solution?

I do admit it's an elegant example, how to use extension methods :)


While first off, I don't see the problem with mid, substring, or looping :)
But, I would definately want something like this encapsulated some how. I
figure a custom extension method looks good - because it appears to be a
member of string, even though it's not. And, it will probably end up being
faster then using a linq type query...

--
Tom Shelton
.



Relevant Pages

  • Re: chop, separate, split a STRING into sections?
    ... Here's a sample of what I'm looking for:  break apart a string into ... DIM strMyInputString as String = "01234567ABCDEFGHIJ" ... and there's no 'single function' or elegant 'one- ... using a For loop and Substring instead of the While loop. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: splitting a CSV line in half
    ... I need to split it such that the first 4 fields are in one String and the rest in the other: ... Can anyone suggest a more elegant technique? ... a couple of substring() calls to extract the two halves. ... four fields and three commas in one group, ...
    (comp.lang.java.programmer)
  • Extraction of attribute values from pattern matches
    ... My program has to deal with a lot of text of the pattern: ... Strings or a String array. ... String class methods such as 'split' and 'substring' but I was looking ... for a more elegant way to do this, since I need to do this operation in ...
    (comp.lang.java.programmer)
  • 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: Finding 1 of 3 different strings in a string
    ... I doubt if anyone would call this "elegant", but it should do what you want... ... Note the absolute cell addresses used in the example formulas being executed inside the CHOOSE function in response to your 3 error code messages... ... It would nice if Findreturned 0 if the string was ...
    (microsoft.public.excel.worksheet.functions)