How to convert to uppercase

Hi. Suppose we have a String "trade", how to convert the first character 't' into uppercase, and thus change the string into "Trade"

Another question is about the use of RichTextBox. I would like to highlight a specified word. However, due to its exact matching, some variant forms of the specified word can not be highlighted. For example, if the specified word is "trade", then "Trade" or "TRADE" can not be highlighted. Could anybody tell me how to make it possble to highlight every variant of the given word. Thanks in advance.



Answer this question

How to convert to uppercase

  • misiu_mietowy

    If you want to change the entire string to uppercase, you can do myString.ToUpper. If however, you want to only change the first character of each word, you can use the following function:

    Friend Shared Function TitleCase(ByVal unformattedString As String) As String

    Dim myTI As System.Globalization.TextInfo = New System.Globalization.CultureInfo("en-US", False).TextInfo

    Return myTI.ToTitleCase(unformattedString)

    End Function

    Note: this will not work with names such as McDonalds or O'Brien.

    Jim Wooley



  • Rudi Goossens

    Or you could convert to a specific case - say use to .ToLower .ToUpper methods of a string to convert them to all upper case or lower case. if this is only for a specific method when the rest of your code you want to leave case sensitive.

    If the default is case insensitivity then using option compare text to make the casing of the text case insensitive is a better approach than using toupper, tolower everywhere in your code.


  • Canito

    As for the issue of highlighting each instance of a given word in text, you may want to look into the System.Text.RegularExpressions.Replace method. It has an overload that allows you to pass an option to IgnoreCase). It may take a bit to format the regular expression and replacement string.

    Jim Wooley



  • syamantak

    When I was trying to search a particular word, say "trade", in the text of RichTextBox, I wrote the following code:

    RichTextBox1.SelectionStart = RichTextBox1.Find("trade")

    However, the text contains a number of "trade" and the above method can only find the first one. My question is how to highlight all "trade" in the RichTextBox.


  • Eric Lofstrom

    You asked the same question again.

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=304763&SiteID=1

    You'll find a potential answer to your problem. Try it out see if it works and let as know if it does what you need it to do.

    I think the solution will highlight the words by changing to foreground colors of each occurence - so they are highlighted but not selected.


  • Gary Loong

    Option Compare Text will make all of your string comparisons case insensitive. Add that to the top of the code file.


  • How to convert to uppercase