none
How to capitalize first letter of a string? RRS feed

  • Question

  • Hi.  Simple question.  I want to capitalize only the first letter of a word.  Tired
    LastName = LastName.ToUpper
    
    But this results with all letters of the word in uppercase.  Found this one on a Google search:
    variable1 = StrConv(variable1, VbStrConv.ProperCase)
    
    But "StrConv" and "VbStrConv" are not being recognized.
    Any thoughts?
    Sunday, September 25, 2011 5:40 PM

Answers

  • You can put something like this in your Validate method of the table field

    partial void LegalName_Validate(EntityValidationResultsBuilder results)
    {
        if (this.LegalName!=null && this.LegalName.Length>1)
            this.LegalName = char.ToUpper(this.LegalName[0]) + this.LegalName.Substring(1);
    }


    - "Five is right out!"
    • Proposed as answer by WilliamStacey Sunday, September 25, 2011 5:53 PM
    • Marked as answer by RyanHartz Sunday, September 25, 2011 6:24 PM
    Sunday, September 25, 2011 5:53 PM

All replies

  • You can put something like this in your Validate method of the table field

    partial void LegalName_Validate(EntityValidationResultsBuilder results)
    {
        if (this.LegalName!=null && this.LegalName.Length>1)
            this.LegalName = char.ToUpper(this.LegalName[0]) + this.LegalName.Substring(1);
    }


    - "Five is right out!"
    • Proposed as answer by WilliamStacey Sunday, September 25, 2011 5:53 PM
    • Marked as answer by RyanHartz Sunday, September 25, 2011 6:24 PM
    Sunday, September 25, 2011 5:53 PM
  • Thanks Will!  I'm writing in vb, but was able to decifer your C#

    If Not String.IsNullOrEmpty(Me.LastName) Then
                    Me.LastName = Char.ToUpper(Me.LastName(0)) + Me.LastName.Substring(1)
                End If
    

    Appreciate the help!

    Ryan

    Sunday, September 25, 2011 6:27 PM
  • If you want to propercase each word as the user types then checkout this post.
    Sunday, September 25, 2011 11:47 PM
  • I would also note that proper casing is not right in many context. Example, LLC, iSell, II, etc. Forcing this "cure" can be just as bad as not having it.
    - "Five is right out!"
    Monday, September 26, 2011 12:51 AM
  • This works:

    Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(LastName);

    Friday, October 4, 2013 1:02 PM
  • Excellent and brief

    Thursday, August 1, 2019 1:20 PM