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:
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:
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:
The complete snippet is the following:
And that is all.
Happy coding!
The first change all the string in uppercase:
<%=UCase("string")%>
The above gives us the following output:STRING
While<%=LCase("STRING")%>
gives us:string
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:
<%=Len(string)-1%>
We take those characters starting from the right hand side:<%=Right(Len(string)-1)%>
And we make it lowercase:<%=LCase(Right(Len(string)-1))%>
The complete snippet is the following:
<%= UCase(Left(string,1)) & LCase(Right(string, Len(string) - 1))%>
And that is all.
Happy coding!