472,992 Members | 3,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

Adjusting string handling function for exceptions?

No joy on Macromedia's boards after a few days; maybe someone can help me
here.

I got an excellent string handling function off of planet-source-code.com
that converts text strings to proper case, i.e. "the REMAINS of the DAY" is
converted to "The Remains Of The Day".

This function is particularly good in that it handles certain exceptions.
Can you help me make it handle more? I'm interested in two tweaks, one easy,
one hard.

The easy tweak: I have certain acronyms that I'd always like to NOT be
converted into proper case -- I want them all upper. For instance, "ASCII",
"SCSI" and "IDE" shouldn't be rendered as "Ascii" "Scsi" and "Ide". The list
of exceptions is short and I'd be fine hardcoding them into the function.

The hard tweak: Certain "noise words" such as "is" "and" and "but" don't
belong in proper case. I don't want them capitalized at all UNLESS they're
the first word in the string.

Can any stringmeisters take a crack at this? Code appears below. If anyone
can help out with at least the first item, I'd be most appreciative.

-KF
Adjusting string handling function for exceptions?
Jul 19 '05 #1
4 1856
Bah, the code, the code:
<%
' ================================================== ===============
' Reformat "txtName" to proper case.
' Output = String
' ================================================== ===============
Function ProperName(txtName)
Dim txtRest, txtTmp, intSpcPos
txtRest = LCase(txtName)

'Single Quote
intTckPos = InStr(1, txtRest, "'",1)
if intTckPos <> 0 then
txtTmp = UCase(Mid(txtRest, intTckPos + 1, 1))
txtRest = Left(txtRest, intTckPos) & txtTmp & Mid(txtRest, intTckPos
+ 2, Len(txtRest))
txtTmp = ""
end if

'Hypenated
intDshPos = InStr(1, txtRest, "-",1)
if intDshPos <> 0 then
txtTmp = UCase(Mid(txtRest, intDshPos + 1, 1))
txtRest = Left(txtRest, intDshPos) & txtTmp & Mid(txtRest, intDshPos
+ 2, Len(txtRest))
txtTmp = ""
end if

'MC
intMcPos = InStr(1,txtRest,"mc",1)
if intMcPos <> 0 then
txtTmp = UCase(Mid(txtRest, intMcPos+2, 1))
txtRest = Left(txtRest, intMcPos+1) & txtTmp & Mid(txtRest,
intMcPos+3, Len(txtRest))
txtTmp = ""
end if

'Space
intSpcPos = InStr(1, txtRest, " ")
Do While intSpcPos <> 0
txtTmp = txtTmp & UCase(Left(txtRest, 1)) & Mid(txtRest,
2,(intSpcPos - 1))
txtRest = Mid(txtRest, intSpcPos + 1, Len(txtRest))
intSpcPos = InStr(1, txtRest, " ")
Loop

'Output
ProperName = txtTmp & UCase(Left(txtRest, 1)) & Mid(txtRest,
2,Len(txtRest))
End Function
' ================================================== ===============
%>

"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
No joy on Macromedia's boards after a few days; maybe someone can help me
here.

I got an excellent string handling function off of planet-source-code.com
that converts text strings to proper case, i.e. "the REMAINS of the DAY" is converted to "The Remains Of The Day".

This function is particularly good in that it handles certain exceptions.
Can you help me make it handle more? I'm interested in two tweaks, one easy, one hard.

The easy tweak: I have certain acronyms that I'd always like to NOT be
converted into proper case -- I want them all upper. For instance, "ASCII", "SCSI" and "IDE" shouldn't be rendered as "Ascii" "Scsi" and "Ide". The list of exceptions is short and I'd be fine hardcoding them into the function.

The hard tweak: Certain "noise words" such as "is" "and" and "but" don't
belong in proper case. I don't want them capitalized at all UNLESS they're
the first word in the string.

Can any stringmeisters take a crack at this? Code appears below. If anyone
can help out with at least the first item, I'd be most appreciative.

-KF
Adjusting string handling function for exceptions?

Jul 19 '05 #2
"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
No joy on Macromedia's boards after a few days; maybe someone can help me here.

I got an excellent string handling function off of planet-source-code.com that converts text strings to proper case, i.e. "the REMAINS of the DAY" is converted to "The Remains Of The Day".

This function is particularly good in that it handles certain exceptions. Can you help me make it handle more? I'm interested in two tweaks, one easy, one hard.

The easy tweak: I have certain acronyms that I'd always like to NOT be
converted into proper case -- I want them all upper. For instance, "ASCII", "SCSI" and "IDE" shouldn't be rendered as "Ascii" "Scsi" and "Ide". The list of exceptions is short and I'd be fine hardcoding them into the function.
The hard tweak: Certain "noise words" such as "is" "and" and "but" don't belong in proper case. I don't want them capitalized at all UNLESS they're the first word in the string.

Can any stringmeisters take a crack at this? Code appears below. If anyone can help out with at least the first item, I'd be most appreciative.

-KF
Adjusting string handling function for exceptions?


Here's an article:
http://aspfaq.com/show.asp?id=2299

Here's a modified version of the code found in that article:
<script language="JScript" runat="SERVER">
function pCase(s){
//Proper Case
s = s.replace(/(\w)(\w*)/gi,function(m,c1,c2,p,s){return
c1.toUpperCase() + c2.toLowerCase();});
//MC
s = s.replace(/\bmc(\w)/gi,function(m,c1,p,s){return "Mc" +
c1.toUpperCase();});
// Lowers
s = s.replace(/\b(is|and|but)\b/gi,function(m,p,s){return
m.toLowerCase();});
// Uppers (First letter of sentence and abbreviations)
s = s.replace(/^\w|\b(ascii|scsi|ide)\b/gi,function(m,p,s){return
m.toUpperCase();});
return s;
}
Response.Write(pCase('bill o\'reilly') + '<br>');
Response.Write(pCase('bill van der wal') + '<br>');
Response.Write(pCase('mr. moseley-williams') + '<br>');
Response.Write(pCase('Joe VanWyck') + '<br>');
Response.Write(pCase('is rOnAlD mCdOnAlD someone who uses ascii, scsi,
and ide?') + '<br>');
</script>

HTH
-Chris Hohmann
Jul 19 '05 #3
Thanks, Chris. Here's another VBScript solution that's pretty nice; you can
define a list of exceptions within the function:
http://www.15seconds.com/howto/pg000078.htm

-KF

"Chris Hohmann" <no****@thankyou.com> wrote in message
news:uC**************@tk2msftngp13.phx.gbl...
"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
No joy on Macromedia's boards after a few days; maybe someone can help

me
here.

I got an excellent string handling function off of

planet-source-code.com
that converts text strings to proper case, i.e. "the REMAINS of the

DAY" is
converted to "The Remains Of The Day".

This function is particularly good in that it handles certain

exceptions.
Can you help me make it handle more? I'm interested in two tweaks, one

easy,
one hard.

The easy tweak: I have certain acronyms that I'd always like to NOT be
converted into proper case -- I want them all upper. For instance,

"ASCII",
"SCSI" and "IDE" shouldn't be rendered as "Ascii" "Scsi" and "Ide".

The list
of exceptions is short and I'd be fine hardcoding them into the

function.

The hard tweak: Certain "noise words" such as "is" "and" and "but"

don't
belong in proper case. I don't want them capitalized at all UNLESS

they're
the first word in the string.

Can any stringmeisters take a crack at this? Code appears below. If

anyone
can help out with at least the first item, I'd be most appreciative.

-KF
Adjusting string handling function for exceptions?


Here's an article:
http://aspfaq.com/show.asp?id=2299

Here's a modified version of the code found in that article:
<script language="JScript" runat="SERVER">
function pCase(s){
//Proper Case
s = s.replace(/(\w)(\w*)/gi,function(m,c1,c2,p,s){return
c1.toUpperCase() + c2.toLowerCase();});
//MC
s = s.replace(/\bmc(\w)/gi,function(m,c1,p,s){return "Mc" +
c1.toUpperCase();});
// Lowers
s = s.replace(/\b(is|and|but)\b/gi,function(m,p,s){return
m.toLowerCase();});
// Uppers (First letter of sentence and abbreviations)
s = s.replace(/^\w|\b(ascii|scsi|ide)\b/gi,function(m,p,s){return
m.toUpperCase();});
return s;
}
Response.Write(pCase('bill o\'reilly') + '<br>');
Response.Write(pCase('bill van der wal') + '<br>');
Response.Write(pCase('mr. moseley-williams') + '<br>');
Response.Write(pCase('Joe VanWyck') + '<br>');
Response.Write(pCase('is rOnAlD mCdOnAlD someone who uses ascii, scsi,
and ide?') + '<br>');
</script>

HTH
-Chris Hohmann

Jul 19 '05 #4
"Ken Fine" <ke*****@u.washington.edu> wrote in message
news:OR**************@tk2msftngp13.phx.gbl...
Thanks, Chris. Here's another VBScript solution that's pretty nice; you can define a list of exceptions within the function:
http://www.15seconds.com/howto/pg000078.htm


It does not handle "McDonald". The code is also somewhat more difficult
to follow, IMHO. One thing it does do is check for apostrophes followed
by "s" or "t", like in the abbreviations "it's" and "don't". The same
can be achieved in the code I provided by including 't and 's in the
"Lowers" expression. Here's the new regular expression:

/\b(is|and|but|'t|'s)\b/gi

Please note that this change will fail for names like O'Shawnessey and
O'Toole.

HTH
-Chris Hohmann

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
7
by: Ekim | last post by:
hy, I've a question concerning exception-handling in c++: is there a possibility to catch any exception (I know one can do that by "catch(...)") and display its default-error-message (like in...
11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
5
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite...
3
by: Steve - DND | last post by:
I was wondering if anyone can point me to some articles or practices they use for dealing with errors in their applications; particularly in an n-tier design. I am trying to find one way to...
2
by: Petr Jakes | last post by:
I am a little bit confused by all possibilities for exceptions handling in Python (probably because I am not skilled enough??) I did try to search trough this list and reading Python tutorial about...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
9
by: arnuld | last post by:
Can anyone point me to some online/offline document on error handling in C ? I am doing some Socket Programming in C and feeling a lots of difficult in error handling :( Also some time ago...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.