PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Uppercase first letter in each word of a string.-VBForums
Results 1 to 14 of 14

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

  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.

  2. #2
    Addicted Member
    Join Date
    Jun 2008
    Location
    Macedonia
    Posts
    188

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

    Good code but see this
    vb Code:
    1. Dim s As String = "the quick brown fox jumps over the lazy dog"
    2. Dim s2 As String = StrConv(s, VbStrConv.ProperCase)
    3. MessageBox.Show(s2)

  3. #3

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

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

    Now that just makes my code look like a waste of CPU Usage...
    The first reason i got it out of my signature.

  4. #4
    Addicted Member
    Join Date
    Jun 2008
    Location
    Macedonia
    Posts
    188

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

    Sry man

  5. #5

  6. #6
    Member
    Join Date
    Oct 2011
    Location
    Sri Lanka
    Posts
    40

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

    Please can any one tell me how to do this while the user is entering data to the text box??? How can I modify this in text_changed function?

  7. #7

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

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

    Quote Originally Posted by chathu1234 View Post
    Please can any one tell me how to do this while the user is entering data to the text box??? How can I modify this in text_changed function?
    In the Textbox's keyup event place this code, assuming your textbox is TextBox1:

    Code:
    TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)

  8. #8
    Member
    Join Date
    Oct 2011
    Location
    Sri Lanka
    Posts
    40

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

    Thanks Noah but it will only allow to type backwards.example: if you type "upper case" it will give you "Esa Creppu"
    How can I fix this

  9. #9

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

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

    You have to set the Selection of the Textbox to the length of the Textbox's value minus 1.

    e.g.

    Code:
    TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
    TextBox1.SelectionStart = TextBox1.Length - 1
    What is happening is that when the Textbox's value is changed the Selection of the Textbox is being reset to 0.

  10. #10
    Member
    Join Date
    Oct 2011
    Location
    Sri Lanka
    Posts
    40

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

    Thank you Noah for taking your time to help me.But your code did not resolve the problem.Now when I enter the first letter the cursor jumps back to the beginning of the text.from then on in it works fine.
    Ex: Input - upper case
    Output - Pper Caseu
    Can you help me please?
    Im using VB 2008 and vb2010.Both do not support textbox.length
    Had to use textbox.TextLength

    Thank you so much
    Last edited by chathu1234; Oct 18th, 2011 at 12:16 AM. Reason: Got the name wrong

  11. #11
    Member
    Join Date
    Oct 2011
    Location
    Sri Lanka
    Posts
    40

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

    Quote Originally Posted by noahssite View Post
    You have to set the Selection of the Textbox to the length of the Textbox's value minus 1.

    e.g.

    Code:
    TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
    TextBox1.SelectionStart = TextBox1.Length - 1
    What is happening is that when the Textbox's value is changed the Selection of the Textbox is being reset to 0.
    I figured it out Noah.
    Instead of
    Code:
    TextBox1.SelectionStart = TextBox1.Length - 1
    I used
    Code:
    TextBox1.SelectionStart = TextBox1.TextLength + 1
    It solved the issue.Thank you so much

  12. #12

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

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

    Oh my mistake.

    This should have worked, I believe:

    Code:
    TextBox1.SelectionStart = TextBox1.Length
    with no adding/subtracting.
    Last edited by noahssite; Oct 20th, 2011 at 08:19 AM.

  13. #13
    New Member
    Join Date
    Nov 2012
    Posts
    1

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

    Quote Originally Posted by noahssite View Post

    vb Code:
    1. MyString = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(MyString)
    thanks!

  14. #14
    Registered User
    Join Date
    Aug 2019
    Posts
    1

    Red face Re: Uppercase first letter in each word of a string.

    Quote Originally Posted by stru4nak View Post
    Good code but see this
    vb Code:
    1. Dim s As String = "the quick brown fox jumps over the lazy dog"
    2. Dim s2 As String = StrConv(s, VbStrConv.ProperCase)
    3. MessageBox.Show(s2)
    Thank you very much sir

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width