How can I take a string and make the first letter a capital and all others lower case?
I’ve seen this somewhere before, but It always seems faster to ask you guys
How can I take a string and make the first letter a capital and all others lower case?
I’ve seen this somewhere before, but It always seems faster to ask you guys
I should mention that the data I am trying to transform is all UPPER CASE. I am suspecting that there will be anomolies (sp?) in the data, of course.
Here’s what I am coming up with. I hope I am on the right track
<%
varString = "SITEPOINT"
Function CaseConvert(varString)
LCase(varString) 'will convert to lowercase
UCase(Left(varString, 1)) 'will convert 1st to UC
End Function
%>
Try this thread, it contains alot of functions, mostly of mine http://www.sitepointforums.com/showthread.php?s=&threadid=31119
If you don’t find what your looking, PM me
I think I found it!
'makeCap function
'***************************************************************************************************
'Makes first letter uppercase, rest lowercase
'usage: response.write(makeCap("testing"))
function makeCap(str)
If len(trim(str)) > 2 then
fLetter = left(str, 1)
fLetter = uCase(fLetter)
makeCap = fLetter & right(str, (len(str) - 1))
end if
end function
Thanks D’!
I just noticed that it makes the first letter capital assuimng that all other letters are lower case.
In my case I need to convert all others to lower case. So, I will have to modify this function a bit.
makeCap = fLetter & LCase(right(str, (len(str) - 1)))
Now it works like it should! Here’s a question. If your string has two words, say ‘New Balance’, how do you make each word start with a capital letter?
No problem moo, that’s a function of mine that jeremy posted
OK here we go. Answer to my 2nd question:
“How do I make all words in the string CAPITAL and the others lowercase?”
Source: http://www.programmersresource.com/tips/tip101.asp
Function ChkString(InString)
if InString = "" then InString = " "
InString = Replace(InString, "'","''")
FoundSpace = True
OutputString = ""
for MidPosition = 1 to len(InString)
if FoundSpace = true then
OutputString = OutputString & UCase(Mid(InString,MidPosition,1))
FoundSpace = false
else
OutputString = OutputString & LCase(Mid(InString,MidPosition,1))
if Mid(InString,MidPosition,1) = " " then FoundSpace = true
if Mid(InString,MidPosition,1) = chr(10) then FoundSpace = true
end if
next
ChkString = OutputString
End Function
<cents id=“2”>All I can say is that is some FUgly code you have there. I have seen just about enough of that kind of crap code to motivate me to open source my function library, which contains over 200 functions to do various things in ASP/VBScript.</cents>
Are you talkin about my code, kode?
'makeCap function
'***************************************************************************************************
'Makes first letter uppercase, rest lowercase
'usage: response.write(makeCap("testing"))
function makeCap(str)
If len(trim(str)) > 2 then
fLetter = left(str, 1)
fLetter = uCase(fLetter)
makeCap = fLetter & right(str, (len(str) - 1))
end if
end function
?
If that is your code, then, YES.
What’s wrong with it?
Problem #1 is that your function parameter (str) is a reserved word. While it won’t error, it shows bad coding techniques. Number 2 is that generally speaking, the code just looks like crap. This is my version of your function, keeping in mind that I would never write this function this way.
Public Function makeCap( strVariable )
If Len( Trim( strVariable ) ) > 2 Then
fLetter = Left( strVariable, 1 )
fLetter = UCase( fLetter )
makeCap = fLetter & Right( strVariable, Len( strVariable ) - 1 )
End If
End Function
Isn’t there a PCase statement in ASP???
Is this better Kode?
'makeCap function
'***************************************************************************************************
'Makes first letter uppercase, rest lowercase
'usage: response.write(makeCap("testing"))
function makeCap(str)
makecap = UCase(left(str, 1)) & right(str, (len(str) - 1))
end function
Personally I didn’t see anything wrong with it. There are shorter ways to do everything, but sometimes those need to be weighed against readability…
Hey, Kode…You’re not an ex-mainframer or assembler coder, are you? You’re by far one of the most militant on coding style that I’ve seen since my mainframe teacher in college??
There is no PCase() function in VBA or VBScript.
Here is my version of this function, which takes into account a few variables that most people wouldn’t think of.
Public Function ProperCase( ByVal strVariable )
strVariable = Trim( strVariable )
If Len( strVariable ) > 2 Then
If Not IsNumeric( strVariable ) And Not IsArray( strVariable ) Then
strReply = UCase( Left( strVariable, 1 ) ) & _
LCase( Right( strVariable, Len( strVariable ) - 1 ) )
Else
strReply = strVariable
End If
End If
ProperCase = strReply
End Function
As for my past, I have never done assembly or mainframe work, nor did I learn this in college, but I have been coding for almost 14 years. My code is my life - I do it for a living, a hobby, a career, a company, and I dream it (when I sleep, which isn’t often). Just like bakers wouldn’t serve a cake that has fallen, I will not write or use code that is not the best it can possibly be. On an average day, I will code in at least 7 languages, not including HTML, CSS and other markup languages. Without good coding style for each language, and a common set of “morals”, there would be no way that one person could switch languages that often.
Beyond my own personal coding morals, there is the greater view. Code that is written badly causes 85% of the errors you will ever see in ASP. Bad code also slows down the ASP execution process. I run 5 NT servers, and I get to watch the logs, every time ASP is executed, I can see it, see how long it took to parse each script, etc. While most people don’t have the access to servers that I do, they should still take into account that ASP is server executed, and if 10 people load a badly coded page in 1 second, that is hell on the server. Most people think that ASP is a slow language, but in reality, it is very fast, but bad coding makes it crawl.
My general mission in my ASP life is to improve the globally accepted standard of code. By introducing a higher standard of code, it will
A) …eliminate the “developers” who code it just because they can read it from a book. (I HATE WROX books, I might add, they are so disgusting.)
B) …decrease the load on NT servers in general, and increase the speed of content delivery to the user.
C) …make is much easier for developer to work together on projects, and/or share code in other ways.
While I may seem militant, I am not. You are seeing my passion, one of the few times anyone will see any form of emotion from me.
Thanks for the run down of your experience, Kode. I hope you didn’t take from my post that I was insulting you. I was just curious because I noticed how passionate you are about coding style and you just reminded me of a teacher I have in college who would deduct points for style. His motto was “if you can do it in ten lines or two and get the same result and still have readable code, you better do it in two.” I picked up a lot from his class, and that knowledge and coding style has enabled me to transition from all the different platforms and languages that I have worked in. I just don’t have enough experience(YET) with the nitty gritty of ASP (global.asa, sessions and such) to be able to pick out the trouble spots behind code coming at me).
BTW, in your ProperCase code(which was very enlightening because I would never have thought about numerics or arrays <slaps self in head>), you start out using strVariable, then move it to strReply and return that. Is that intentional, because if you have a one position field, you will return a blank value instead of the original value passed, which I don’t think was the intent. Am I missing something??
Dave, good point @ returning data, was not intentional to leave out my last Else statement. As for style, I don’t care what style my developers have, so long as they meet my specifications, and since I have trained most of them, they end up using my style anyway. LOL.
Since you mentioned global.asa, you opened a WHOLE other kan of “Kode is gonna woop up on your arse”. Only one site on any of my NT servers uses global.asa, and I only allow that site to do so because they SWORE to me that they would be rebuilding it soon (JEREMY!) hehehehe. I have told clients that I would not host their site because their developer’s code was insecure, used unneeded includes, etc. Most of my clients understand that I HAVE to be anal about the security part, since they know that my primary service is secure document archive backup for offices, but I have had a few get quite miffed.
Incidentally, I didn’t feel offended or insulted at all. I came on strong, and you were simply wanting some inside information in order to understand my perspective, and I respect you for doing so. I actually expected your next response to be “How in the /****/ing hell can you have been coding for 14 years?” LOL, but I guess you didn’t look at my profile.
Originally posted by KodeKrash
Incidentally, I didn’t feel offended or insulted at all. I came on strong, and you were simply wanting some inside information in order to understand my perspective, and I respect you for doing so. I actually expected your next response to be “How in the /****/ing hell can you have been coding for 14 years?” LOL, but I guess you didn’t look at my profile.
Now that you made me curious, I did go out and check that out. You’re twenty two. That means you’ve been coding since you were 8. I can believe that. If you want to look at it like that, you can say I’ve been coding for 20-21 years(we won’t tell you on what kind of machine THAT was though:D).
KodeKrash,
Why are you applying the Trim function to the parameter before you’ve checked the datatype? Trying to Trim an array will cause an error. (I couldn’t help but pick you up on that. )
Also, you haven’t declared the local variables either which seems slightly at odds with your strict coding style.
I’ve been looking at some of your scripts (from your Intelidev site) the last couple of days and noticed that your don’t use many comments in these scripts and you don’t often use standard vb coding conventions (eg prefixes for global and class variables). Surely one of the main reasons for using coding standards are so that other coders can look through an unfamilar piece of code without first trying to get used to the coding style used, and using standard language coding conventions and comments aid this.
I’m not trying to pick a fight here, I just wanted to hear the reasoning for some of your styles.
Just to make things clear, I don’t make any claims to write the best or cleanest of code and I know I’ve posted some ugly code on these forums.
Dave,
I started learning BASIC when I was 6, on a TI 4-99a, which I still have. The weekend of my 7th birthday, my dad brought me a box of PC parts, and I built my first IBM clone, an 8086 with 640kb of RAM and 2 360kb floppy drives and an EGA disply, and a 300 baud modem, which I used to BBS around. The rest of my school life was like that, which the exception of high school, which is when I trained as a Freightliner mechanic, and only used a computer to do school work. In my junior year of high school, my brother introduced me to the web. That was in 1997, at which time I commenced to educate myself in the various languages of the web. Now, I am 21 (not 22), and feel pretty happy with my repetoire (sp!) of languages.
Shane,
WOO HOO, it is about time that someone yacked on my code a bit. I will make a lengthier reply to the code issues(s) and style questions when I return.
Originally posted by KodeKrash
Dave,
I started learning BASIC when I was 6, on a TI 4-99a,
Just my insignificant submission: that’s more properly TI 99/4A see: http://www.netten.net/~garycox/ti99.htm
My catch cause I had one, even had the Extended Basic cart.
Bob