Adjusting string handling function for exceptions?

Discussion in 'ASP General' started by Ken Fine, Apr 4, 2004.

  1. Ken Fine

    Ken Fine Guest

    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?
     
    Ken Fine, Apr 4, 2004
    #1
    1. Advertising

  2. Ken Fine

    Ken Fine Guest

    code Re: Adjusting string handling function for exceptions?

    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" <> wrote in message
    news:%23g$...
    > 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?
    >
    >
     
    Ken Fine, Apr 4, 2004
    #2
    1. Advertising

  3. "Ken Fine" <> wrote in message
    news:%23g$...
    > 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
     
    Chris Hohmann, Apr 4, 2004
    #3
  4. Ken Fine

    Ken Fine Guest

    SOLVED/SOLUTION FOUND Re: Adjusting string handling function for exceptions?

    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" <> wrote in message
    news:...
    > "Ken Fine" <> wrote in message
    > news:%23g$...
    > > 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
    >
    >
     
    Ken Fine, Apr 4, 2004
    #4
  5. Re: SOLVED/SOLUTION FOUND Re: Adjusting string handling function for exceptions?

    "Ken Fine" <> wrote in message
    news:OR$...
    > 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
     
    Chris Hohmann, Apr 5, 2004
    #5
  6. Ken Fine

    zhuzijie Guest

    Ò»¸öºÃµÄftpÍøÕ¾

    ²»´í¡¡²»´í
    Chris Hohmann <> wrote in message
    news:...
    > "Ken Fine" <> wrote in message
    > news:OR$...
    > > 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
    >
    >
    >
     
    zhuzijie, Apr 8, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    5
    Views:
    30,039
    Chris Smith
    Jul 14, 2004
  2. Paul Miller
    Replies:
    3
    Views:
    1,024
    Alex Martelli
    Nov 12, 2003
  3. Replies:
    3
    Views:
    613
    Sherm Pendley
    Apr 16, 2007
  4. Silfheed
    Replies:
    4
    Views:
    287
    Silfheed
    Aug 21, 2007
  5. Lie
    Replies:
    3
    Views:
    635
Loading...

Share This Page