VB icon

A Better Proper Case Function

Email
Submitted on: 1/6/2015 10:28:00 PM
By: Doug Tyson (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: ASP (Active Server Pages)
Views: 4035
 
     Just copy/paste this function into your code, and it will allow you to convert a string to proper case. Now you've got UCase, LCase, AND PCase.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: A Better Proper Case Function
' Description:Just copy/paste this function into your code, and it will allow you to convert a string to proper case. Now you've got UCase, LCase, AND PCase.
' By: Doug Tyson (from psc cd)
'
' Assumes:I tried to account for as many "unimportant" words as I could, but I'm sure I've missed some. Just add any entries you feel necessary in the select statement.
'**************************************

Function PCase(strInput)
	'Variable declaration.
	dim strArr
	dim tmpWord
	dim tmpString
	dim last
	
	
	'Create an array to store each word in the string separately.
	strArr = split(strInput," ")
	
	
	if ubound(strArr) > 0 then
		for x = lbound(strArr) to ubound(strArr)
			'Set each word to lower case initially.
			strArr(x) = LCase(strArr(x))
	
			
			'Skip the unimportant words.
			select case strArr(x)
				case "a"
				case "an"
				case "and"
				case "but"
				case "by"
				case "for"
				case "in"
				case "into"
				case "is"
				case "of"
				case "off"
				case "on"
				case "onto"
				case "or"
				case "the"
				case "to"
				case "a.m."
					strArr(x) = "A.M."
				case "p.m."
					strArr(x) = "P.M."
				case "b.c."
					strArr(x) = "B.C."
				case "a.d."
					strArr(x) = "A.D."
				case else
				
					'Capitalize the first letter, but don't forget to take into account that
					'the string may be in single or double quotes.
					if len(strArr(x)) > 1 then
						if mid(strArr(x),1,1) = "'" or mid(strArr(x),1,1) = """" then
							tmpWord = mid(strArr(x),1,1) & Ucase(mid(strArr(x),2,1)) & mid(strArr(x),3,len(strArr(x))-2)
						else
							tmpWord = Ucase(mid(strArr(x),1,1)) & mid(strArr(x),2,len(strArr(x))-1)
						end if
						strArr(x) = tmpWord
					end if
					
			end select
			
			
			'The unimportant words may need to be capitalized if they follow a dash, colon,
			'semi-colon, single quote or double quote.
			if x > 0 then
				if instr(strArr(x-1),"-") _
				or instr(strArr(x-1),":") _
				or instr(strArr(x-1),";") then
					tmpWord = Ucase(mid(strArr(x),1,1)) & mid(strArr(x),2,len(strArr(x))-1)
					strArr(x) = tmpWord
				end if
			end if
			
		next
	else
		strArr(0) = LCase(strArr(0))
	end if
	
	
	'Make sure the first word in the array is upper case, but don't forget to take into account
	'that the string may be in single or double quotes.
	if mid(strArr(0),1,1) = "'" or mid(strArr(0),1,1) = """" then
		tmpWord = mid(strArr(0),1,1) & Ucase(mid(strArr(0),2,1)) & mid(strArr(0),3,len(strArr(0))-2)
	else
		tmpWord = Ucase(mid(strArr(0),1,1)) & mid(strArr(0),2,len(strArr(0))-1)
	end if
	strArr(0) = tmpWord
	
	
	'Also, make sure the last word in the array is upper case, but don't forget to take into account
	'that the string may be in single or double quotes.
	last = ubound(strArr)
	if mid(strArr(last),1,1) = "'" or mid(strArr(last),1,1) = """" then
		tmpWord = mid(strArr(last),1,1) & Ucase(mid(strArr(last),2,1)) & mid(strArr(0),3,len(strArr(last))-2)
	else
		tmpWord = Ucase(mid(strArr(last),1,1)) & mid(strArr(last),2,len(strArr(last))-1)
	end if
	strArr(last) = tmpWord
	
	
	'Rebuild the whole string from the array parts.
	for x = lbound(strArr) to ubound(strArr)
		tmpString = tmpString & strArr(x) & " "
	next
	
	PCase = tmpString
End Function


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.