I'm trying to make a simple script in vb that will transform a string "These are my characters" into this "tHeSe ArE mY cHaRaCtErS"
It seemed simple at first, I created a userform and with 2 textboxes (input and output) and a button. I wanted to split the string into characters firist then alternate between using UCase and LCase with the helf of Mod2 and a loop.
I asked my friend for how would he do it in his preferred language he gave me this in javascript
string.split('').map((c,i) => (i%2)? c.toUppercase() : c.toLowercase() )
and this in ruby
string.split('').each_with_index.map
{ |c, i| return c.uppercase if (i%2).zero? Else c.downcase}.join ''
That's when I thought that .map function is way easier for splitting a string and performing operation on each one.
So my question how would I go about replacing .map used in those languages in VB? thank you in advance.
It is the Select method, for further info please have a look here.
In order you use this method, you have to add the following statement at the beginning of your source code file:
Imports System.Linq
In this namespace many extension methods on IEnumerable(Of T) are defined. One of them is the Select method. Since String implements IEnumerable(Of Char) these methods can be also applied to a String. Select can be used for projecting the elements of a sequence of items as you do so in other languages using map.
e.g. (courtesy of Andrew Morton - check comments)
New String(s.Select(Function(c, i) If(i Mod 2 = 0, Char.ToLower(c), Char.ToUpper(c))).ToArray())
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