Results 1 to 14 of 14

Thread: Uppercase first letter in each word of a string.

Threaded View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Uppercase first letter in each word of a string.

    Here is very simple code to uppercase the first letter of each word in a string.

    What i have done in this code is stored all the words into an array. Then by looping through the array the word is being split in two, the first part of the substring is the first letter. The first letter is then capitalized. After that is done the word is joined back together to make a whole. This process is done for every word in the array. As the process takes action the new edited word replaces the old word in the same array. The words from the array are being placed on a single line which will become the new value of Textbox1.

    vb Code:
    1. 'Try something like so:
    2.         Dim words() As String
    3.         words = TextBox1.Text.Split(" "c)
    4.         For x As Integer = 0 To words.Rank + 1
    5.             words(x) = (words(x).Substring(0, 1).ToUpper & words(x).Substring(1, words(x).Length - 1))
    6.         Next
    7.         Dim NewString As String = String.Empty
    8.         For i As Integer = 0 To words.Rank + 1
    9.             If i = 0 Then
    10.                 NewString = (words(i))
    11.             Else
    12.                 NewString = (NewString & " " & words(i))
    13.             End If
    14.         Next
    15.         TextBox1.Text = NewString


    EDIT: Here is an even simpilar code that will produce the same result as my code above.
    vb Code:
    1. MyString = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(MyString)
    Last edited by noahssite; Feb 14th, 2009 at 08:26 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width