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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hyphenated Names and Apostrophes 2

Status
Not open for further replies.

DougInCanada

Technical User
Feb 1, 2004
98
CA
I have a script for creating user accounts which makes the first letter of a name uppercase and the rest lowercase:

Code:
intLastName = Len(strLastName)
strFirstLetter = UCase(Left(strLastName, 1))
strRemainingLetters = LCase(Right(strLastName, intLastName - 1))
strLastName = strFirstLetter & strRemainingLetters

However, we actually have a situation where a user has not only a hypenated name, but that hypenated name also contains an apostrophe

(lastname = Hoffa-O'Reilly)

I'm looking to add code to my script that will allow me to not only change the 'H' to an uppercase, but also the letters before and after the apostrophe ('O' & 'R') and I'm not quite sure how to go about it....
 
Get the chunk of text starting the character before and after the apostrophe. Then replace that chunk in strLastName with the ucase() of the chunk.

Code:
[s][/s]
intPos = inStr(strLastName, "'")
if (intPos) then
   strChunck = mid(strLastName, intPos -1, 3)
   strLastName = replace(strLastName, strChunck, ucase(strChunck))
end if

-Geates
 
Thanks, Geates, works great!

Just to make it a little more challenging, how can I change it to detect more than one apostrophe in a hyphenated name (ie: O'Reilly-O'Shea)?
 
Here's a solution using RegExp (actually using quite an obscure feature of RegExp: the ability to use a custom replacer function). As you can see it can capitalise multiple names in one go:

Code:
[blue]Dim myReplacer 
 
Set myReplacer = getRef("ReplacerFunction") 

strLastName="hoffa-o'reilly" & vbcrlf &  "wood-robinson" 

With Createobject("vbscript.regexp")
    .Global = True
	.Multiline = True
	.pattern="(^|-|')(\w{1})"
    MsgBox .Replace(strLastName, myReplacer)
End With

	
Public Function ReplacerFunction(wholematchstring,match1,match2,matchpos,source) ',c,d) As String
	ReplacerFunction = match1 + ucase(match2)
End Function[/blue]
 
how can I change it to detect more than one apostrophe in a hyphenated name
Loop it?

Code:
strLastName = "henry-o'rielly-o'brian"

intPos = inStr(strLastName, "'")
do while (intPos)
   strChunck = mid(strLastName, intPos -1, 3)
   strLastName = replace(strLastName, strChunck, ucase(strChunck))
   intPos = inStr(intPos + 1, strLastName, "'")
loop

msgbox strLastName

-Geates
 
>more than one apostrophe in a hyphenated name

The regexp solution already handles that ... :)
 
Thanks to you both, each solution works really well.

strongm, that's really interesting, it's my first encounter with regexp, seems like lots of useful opportunites, but how do I actually replace strLastName with the .replace results, rather than just display them in a message box?

Cheers to you both!
 
thanks, strongm.

Now I just have to try to figure out how to use regexp to capture Mc and Mac (ie: McDuff and MacDonald)....
 
I'd try this:
Code:
.pattern="(^|-|'|Mc|Mac)(\w{1})"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I wouldn't

I'd try:

Code:
[blue].pattern = "(^mc|^mac|^|-|')(\w{1})[/blue]

and modify the replace function as:

Code:
[blue]Public Function ReplacerFunction(wholematchstring,match1,match2,matchpos,source) 
	[b]if len(match1)>1 then [green]'deal with inconvenient fact that the Mcs and Macs are longer than one char[/green]
		match1=ucase(left(match1,1)) & trim(right(match1 & " ", len(match1)))
	end if[/b]	ReplacerFunction = match1 & ucase(match2)
End Function[/blue]
 
Note: for multiple considerations multiple times with multiple objects, the regex solution is WAY more simple and efficient.

-Geates
 
Phenomenal, completely changes how I can deal with strings containing patterns! No, if I can just get a handle on the pattern syntax...found this site:

[link ][/url]

Seems to be quite good for explaining patterns, as well as the other syntax.

Hope others find this as useful as I did.

Nicely done, StrongM and thanks, Geates!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top