Michael
Michael

Reputation: 21

Extract char by sequence in a string vb 2010

I have a text in richtextbox1, I want to extract each 3 char of the string and so on, like:

richtextbox1.text = "It's Chrismas time"  

Result, Sequence of 3 from Right to Left: "Isrmte"
Result, Sequence of 3 from Left to Right: "etmrsI"

How can I do it ?

Code can be in C# 2010. I will translate to vb.net 2010.

Upvotes: 2

Views: 327

Answers (1)

Gabe
Gabe

Reputation: 86708

How about richtextbox1.text.Where((c, i) => i % 3 == 0)?
Or RTL: richtextbox1.text.Reverse().Where((c, i) => i % 3 == 0)
In VB: richtextbox1.text.Where(Function(c, i) i Mod 3 = 0)

However, your example does not show extracting every third character -- your example ignores whitespace, which you might do this way:

text.Where(Function(c) Not Char.IsWhiteSpace(c)).Where(Function(c, i) i Mod 3 = 0)

To take the text and put it into another textbox, you could do this:

textbox2.text = String.Join("", textbox1.text
               .Where(Function(c) Not Char.IsWhiteSpace(c))
               .Where(Function(c, i) i Mod 3 = 0))

Here's my test code copied directly from VS 2010:

    Dim text = "It's Chrismas time"
    Console.WriteLine(String.Join("",
                    text.Where(Function(c) Not Char.IsWhiteSpace(c)) _
                        .Where(Function(c, i) i Mod 3 = 0)))

Upvotes: 3

Related Questions