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:
<%=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!
Last line should read :
ReplyDelete<%= UCase(Left(string,1)) & LCase(Right(string, Len(string)-1))%>
ooopsss... my mistake!
DeleteI change everythings...
Thanks for spotting it!
Hi Marco,
ReplyDeleteI create a function with your script.
Function UpperCase(Txt)
If (Txt) <> "" Then
UpperCase = UCase(Left(Txt,1)) & LCase(Right(Txt, Len(Txt) - 1))
Else
UpperCase = Null
End If
End Function
In the form I use:
input name="subject" type="text" value="<%=UpperCase(rsRecord("subject"))%>
Thanks StillAsp.
Deletea shot var. UCase(Left(text,1)) & Mid(text,2)
ReplyDeleteWhat About if we have "Mens Cloth"
ReplyDelete