user3351921
user3351921

Reputation: 11

Display the first word of input string in UpperCase letters in VB.NET?

How to display the first word of input string in uppercase letters. Display with using message box.

Example:

inputed String = Advance Programing using VB.Net

the function should display = ADVANCE

Upvotes: 1

Views: 875

Answers (2)

Arthur Rey
Arthur Rey

Reputation: 3058

Public Function GetFirstWordUpperCase(ByVal input As String) As String
    Return If(String.IsNullOrEmpty(input) Or String.IsNullOrWhiteSpace(input), Nothing, input.Split()(0).ToUpper())
End Function

Checks if the input string is null, empty or whitespace then returns what you wanted if it is not.

For instance, GetFirstWordUpperCase("how are you") returns "HOW"

Simply do MsgBox(GetFirstWordUpperCase("how are you")) to display the result.

Btw instead of returning Nothing, you could throw an error and catch it where you use the function, it's just the basic idea.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460158

Dim words = input.Split() 
Dim result = String.Format("{0} {1}",
                           words(0).ToUpper(),
                           String.Join(" ", words.Skip(1)))

Edit: oh, just the first word, then use words(0).ToUpper

Upvotes: 3

Related Questions