Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capitalizing first letter only with vbscript "HELLO" --> "Hell

Status
Not open for further replies.

kev510

Programmer
Jul 12, 2006
61
Hi everyone.

I am a newbie to VBscript and searched everywhere for an easy way to achieve my goal. I have found ways but they are very inconvenient to use. Are there any simple step method to capitalize JUST the first letter of a string?

For example -->

"myNaMe"

to

"Myname"

Please help! Thanks!
 
Hi

Strangely enough there isn't a built in function to do this in vbscript. However here is a useful function I found that will do what you want (and also capitalise the first letter of each word in a sentence that you give it).

Code:
Function PCase(strInput)
	Dim iPosition  ' Our current position in the string (First character = 1)
	Dim iSpace     ' The position of the next space after our iPosition
	Dim strOutput  ' Our temporary string used to build the function's output

	' Set our position variable to the start of the string.
	iPosition = 1
	
	' We loop through the string checking for spaces.
	' If there are unhandled spaces left, we handle them...
	Do While InStr(iPosition, strInput, " ", 1) <> 0
		' To begin with, we find the position of the offending space.
		iSpace = InStr(iPosition, strInput, " ", 1)
		
		' We uppercase (and append to our output) the first character after
		' the space which was handled by the previous run through the loop.
		strOutput = strOutput & UCase(Mid(strInput, iPosition, 1))
		
		' We lowercase (and append to our output) the rest of the string
		' up to and including the current space.
		strOutput = strOutput & LCase(Mid(strInput, iPosition + 1, iSpace - iPosition))

		' Note:
		' The above line is something you may wish to change to not convert
		' everything to lowercase.  Currently things like "McCarthy" end up
		' as "Mccarthy", but if you do change it, it won't fix things like
		' ALL CAPS.  I don't see an easy compromise so I simply did it the
		' way I'd expect it to work and the way the VB command
		' StrConv(string, vbProperCase) works.  Any other functionality is
		' left "as an exercise for the reader!"
		
		' Set our location to start looking for spaces to the
		' position immediately after the last space.
		iPosition = iSpace + 1
	Loop

	' Because we loop until there are no more spaces, it leaves us
	' with the last word uncapitalized so we handle that here.
	' This also takes care of capitalizing single word strings.
	' It's the same as the two lines inside the loop except the
	' second line LCase's to the end and not to the next space.
	strOutput = strOutput & UCase(Mid(strInput, iPosition, 1))
	strOutput = strOutput & LCase(Mid(strInput, iPosition + 1))

	' That's it - Set our return value and exit
	PCase = strOutput
End Function

so you would just use

Code:
Reponse.Write(pCase("myname"))

Would write out: Myname

Nick (Webmaster)

 
wow, thank you so much HowardMarks!

it works great!
 
Or to do it without a function:

strTest = "HELLO"
WScript.Echo UCase(Left(strTest, 1)) & LCase(Mid(strTest, 2))

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top