Does anyone have a good Proper Case algorithm

前端 未结 13 1217
一生所求
一生所求 2020-12-15 04:03

Does anyone have a trusted Proper Case or PCase algorithm (similar to a UCase or Upper)? I\'m looking for something that takes a value such as \"GEORGE BURDELL\"

13条回答
  •  Happy的楠姐
    2020-12-15 05:08

    There's also this neat Perl script for title-casing text.

    http://daringfireball.net/2008/08/title_case_update

    #!/usr/bin/perl
    
    #     This filter changes all words to Title Caps, and attempts to be clever
    # about *un*capitalizing small words like a/an/the in the input.
    #
    # The list of "small words" which are not capped comes from
    # the New York Times Manual of Style, plus 'vs' and 'v'. 
    #
    # 10 May 2008
    # Original version by John Gruber:
    # http://daringfireball.net/2008/05/title_case
    #
    # 28 July 2008
    # Re-written and much improved by Aristotle Pagaltzis:
    # http://plasmasturm.org/code/titlecase/
    #
    #   Full change log at __END__.
    #
    # License: http://www.opensource.org/licenses/mit-license.php
    #
    
    
    use strict;
    use warnings;
    use utf8;
    use open qw( :encoding(UTF-8) :std );
    
    
    my @small_words = qw( (? ) {
      s{\A\s+}{}, s{\s+\z}{};
    
      $_ = lc $_ if not /[[:lower:]]/;
    
      s{
          \b (_*) (?:
              ( (?<=[ ][/\\]) [[:alpha:]]+ [-_[:alpha:]/\\]+ |   # file path or
                [-_[:alpha:]]+ [@.:] [-_[:alpha:]@.:/]+ $apos )  # URL, domain, or email
              |
              ( (?i: $small_re ) $apos )                         # or small word (case-insensitive)
              |
              ( [[:alpha:]] [[:lower:]'’()\[\]{}]* $apos )       # or word w/o internal caps
              |
              ( [[:alpha:]] [[:alpha:]'’()\[\]{}]* $apos )       # or some other word
          ) (_*) \b
      }{
          $1 . (
            defined $2 ? $2         # preserve URL, domain, or email
          : defined $3 ? "\L$3"     # lowercase small word
          : defined $4 ? "\u\L$4"   # capitalize word w/o internal caps
          : $5                      # preserve other kinds of word
          ) . $6
      }xeg;
    
    
      # Exceptions for small words: capitalize at start and end of title
      s{
          (  \A [[:punct:]]*         # start of title...
          |  [:.;?!][ ]+             # or of subsentence...
          |  [ ]['"“‘(\[][ ]*     )  # or of inserted subphrase...
          ( $small_re ) \b           # ... followed by small word
      }{$1\u\L$2}xig;
    
      s{
          \b ( $small_re )      # small word...
          (?= [[:punct:]]* \Z   # ... at the end of the title...
          |   ['"’”)\]] [ ] )   # ... or of an inserted subphrase?
      }{\u\L$1}xig;
    
      # Exceptions for small words in hyphenated compound words
      ## e.g. "in-flight" -> In-Flight
      s{
          \b
          (? "Stand-In" (Stand is already capped at this point)
      s{
          \b
          (?

    But it sounds like by proper case you mean.. for people's names only.

提交回复
热议问题