Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple get initials from string - Visual Basic

Tags:

vb.net

Simple beginner exercise:

There's an input box where you put in your name seperated by spaces, then get the first letter from the first and last name and out put it to a label

I.e (Joe Bob) = JB


I know this could be done with an array, but the exercise is more to using string functions like substring, IndexOf, Remove, Replace etc...

like image 592
Ds.109 Avatar asked Aug 15 '26 04:08

Ds.109


1 Answers

There is the handy string method Split which splits a string at whitespaces by default, if you don't 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)
like image 138
Olivier Jacot-Descombes Avatar answered Aug 17 '26 15:08

Olivier Jacot-Descombes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!