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?
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#)
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With