vb.net – Simple get initials from string – Visual Basic

vb.net – Simple get initials from string – Visual Basic

There is the handy string method Split which splits a string at whitespaces by default, if you dont specify another delimiter.

Dim words As String() = TextBox1.Text.Split()
Dim initials As String = 
For Each word As String In words
    initials &= word(0)
Next

Note: Strings can be indexed as if they were Char arrays. word(0) is the first character of word.

initials &= word(0)

is shorthand for

initials = initials & word(0)

You can try this:

 dim str as String=TextBox1.Text
 Label1.Text=str.Remove(1, str.LastIndexOf( )).Remove(2)

If you want, you can do it in one line:

 Label1.Text = TextBox1.Text.Remove(1, TextBox1.Text.LastIndexOf( )).Remove(2)

vb.net – Simple get initials from string – Visual Basic

Could try something like this too!

Dim str As String = textBox1.Text
Dim initials As String = New String(str.Split( c).Select(Function(f) f(0)).ToArray)

Leave a Reply

Your email address will not be published.