3

I have a string like s = "abcdefgh". I want to split it by number of characters like:

a(0)="ab"
a(1)="cd"
a(2)="ef"
a(3)="gh"

Can someone tell me how to do this?

3
  • What is your criteria for splitting? Every time the character changes? Every second character? Commented Jul 26, 2012 at 18:27
  • 3
    Example isn't clear. Do you want to split per 2 chars or split whenever the next char isn't identical to the previous one? Commented Jul 26, 2012 at 18:27
  • 1
    How would you split "abbcdccceeee", for example? (Your question doesn't say much about what you're trying to do...) Commented Jul 26, 2012 at 18:28

4 Answers 4

2

You can use a regular expression to split into two-character groups:

Dim parts = Regex.Matches(s, ".{2}").Cast(Of Match)().Select(Function(m) m.Value)

Demo: http://ideone.com/ZVL2a (C#)

Sign up to request clarification or add additional context in comments.

3 Comments

The […] around the Select is unnecessary. Otherwise, this is how I’d do it as well. Just pay attention that . doesn’t match every character unless you specify the SingleLine flag.
@KonradRudolph: Thanks. I wrote it in C# first and used an online converter. I'm not very good with VB.Net :) Strangely enough, when I tried to do a VB.Net sample, I got internal errors and a note about System.Linq not being a valid library: ideone.com/KlCAu. Did I do something wrong?
The VB.NET compiler for Mono is unfortunately a lost cause. It is completely unusable. I find that a pity since I originally come from VB but I don’t foresee the situation changing soon so you are essentially forced to use C# if you want to write for non-Windows platforms. That said, your VB code on ideone contains a small error, the part as var is wrong, it should just be For Each part, or alternatively … part As Match (and then the Cast would be unnecessary). But that won’t fix the internal compiler error.
2

Here's a Linq method that doesn't require writing a loop:

    Const splitLength As Integer = 2
    Dim a = Enumerable.Range(0, s.Length \ splitLength).
        Select(Function(i) s.Substring(i * splitLength, splitLength))

3 Comments

+1 I was in the middle of writing the exact same thing. Do realize this will blow up on odd-length strings (as most of the other answers as well)
@lc Are you sure? I tested it on 2, 3, 4, 5 & 6 and it always seemed to work okay. The only problem I found was if the original string's length wasn't an exact multiple of the split length – in which case the last few characters were omitted.
Actually you're right, it won't blow up (I was thinking of using a ceiling function to get the last bit). It will however omit the last short bit as you say.
1

Splitting every 2 chars, is what I think you wanted

    Dim s As String = "aabbccdd"

    For i As Integer = 0 To s.Length - 1 Step 1
        If i Mod 2 = 0 Then
            Console.WriteLine(s.Substring(i, 2))
        End If
    Next

2 Comments

Why don’t you simply increment in steps of 2 instead of using the additional check?
Yea that would be even better
0

I you would use a List(Of String) instead, it simplifies it:

Dim s = "aabbccdde" ' yes, it works also with an odd number of chars '
Dim len = 2
Dim list = New List(Of String)
For i = 0 To s.Length - 1 Step len
    Dim part = If(i + len > s.Length, s.Substring(i), s.Substring(i, len))
    list.Add(part)
Next
Dim result = list.ToArray() ' if you really need that array '

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.