Monday, 19 March 2012

ASP: make the first letter uppercase in a string


There are two ASP functions that allow us to change the case of a string: UCase and LCase.
The first change all the string in uppercase:
 The above gives us the following output:
gives us:

If we don't know what is the case of a string, because, for example, we get it as a query result, how can we make the string with only the first capital letter?

In order to do so, we need to combine the two aforementioned functions, the Left, Right and the Len functions. Len gives us the length of a string as a result, while Left and Right pick a certain number of characters from a string.

First of all we take the first letter of our text and make it a capital letter:
<%= UCase(Left(string,1))%>
Then we need to combine the above with the rest of the text and make it lowercase.
The rest of the text is a certain number of characters starting from the right hand side of the text minus the first letter.
The number of characters is:
We take those characters starting from the right hand side:
And we make it lowercase:

The complete snippet is the following:
<%= UCase(Left(string,1)) & LCase(Right(string, Len(string) - 1))%>

And that is all.
Happy coding!


  1. Last line should read :
    <%= UCase(Left(string,1)) & LCase(Right(string, Len(string)-1))%>

    1. ooopsss... my mistake!
      I change everythings...
      Thanks for spotting it!

  2. Hi Marco,

    I create a function with your script.

    Function UpperCase(Txt)
    If (Txt) <> "" Then
    UpperCase = UCase(Left(Txt,1)) & LCase(Right(Txt, Len(Txt) - 1))
    UpperCase = Null
    End If
    End Function

    In the form I use:

    input name="subject" type="text" value="<%=UpperCase(rsRecord("subject"))%>

  3. a shot var. UCase(Left(text,1)) & Mid(text,2)

  4. What About if we have "Mens Cloth"


Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.