Re: Capitalizing a string?

Subject: Re: Capitalizing a string?
From: "Russell Steven Shawn O'Connor" <roconnor@xxxxxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 9 Dec 1998 19:01:09 -0500 (EST)
On 9 Dec 1998, J-P Theberge wrote:

> Hi,
> 
> Is there a function to capitalize a string?
> 
> "string" => "String"

I have written two routines to convert from "foo-bar" or "foo bar" to
"fooBar or "FooBar".  They are called *studlyCaps* and *StudlyCaps*
respectively and are listed below.  If you truely want to only upcase the
first letter then I think the following will work on the string str:

(string-append (string (char-upcase (string-ref str 0)))
               (substring str 1 (string-length str))
)

(Hmmm,  There is probably a better way of doing that.)

(define (*studlyCapsLoop* strBefore strAfter upcase)
        (cond ((= 0 (string-length strBefore)) strAfter)
              ((or (char=? (string-ref strBefore 0) #\space)
                   (char=? (string-ref strBefore 0) #\-)
               )
               (*studlyCapsLoop*
                (substring strBefore 1 (string-length strBefore))
                strAfter
                #t
               )
              )
              (#t
               (*studlyCapsLoop*
                (substring strBefore 1 (string-length strBefore))
                (string-append strAfter
                 (string ((if upcase char-upcase char-downcase)
                          (string-ref strBefore 0)))
                 )
                #f
               )
              )
        )
)

(define (*studlyCaps* str)
        (*studlyCapsLoop* str "" #f)
)

(define (*StudlyCaps* str)
        (*studlyCapsLoop* str "" #t)


Rumour has it that char-upcase isn't implemented in Jade.  I believe you
can, and should, get the function from the DSSSL cookbook.  But for the
record, here is mine:

(define (char-upcase ch)
        (cond ((char=? #\a ch) #\A)
              ((char=? #\b ch) #\B)
              ((char=? #\c ch) #\C)
              ((char=? #\d ch) #\D)
              ((char=? #\e ch) #\E)
              ((char=? #\f ch) #\F)
              ((char=? #\g ch) #\G)
              ((char=? #\h ch) #\H)
              ((char=? #\i ch) #\I)
              ((char=? #\j ch) #\J)
              ((char=? #\k ch) #\K)
              ((char=? #\l ch) #\L)
              ((char=? #\m ch) #\M)
              ((char=? #\n ch) #\N)
              ((char=? #\o ch) #\O)
              ((char=? #\p ch) #\P)
              ((char=? #\q ch) #\Q)
              ((char=? #\r ch) #\R)
              ((char=? #\s ch) #\S)
              ((char=? #\t ch) #\T)
              ((char=? #\u ch) #\U)
              ((char=? #\v ch) #\V)
              ((char=? #\w ch) #\W)
              ((char=? #\x ch) #\X)
              ((char=? #\y ch) #\Y)
              ((char=? #\z ch) #\Z)
              (#t ch)
       )
)

(define (char-downcase ch)
        (cond ((char=? #\A ch) #\a)
              ((char=? #\B ch) #\b)
              ((char=? #\C ch) #\c)
              ((char=? #\D ch) #\d)
              ((char=? #\E ch) #\e)
              ((char=? #\F ch) #\f)
              ((char=? #\G ch) #\g)
              ((char=? #\H ch) #\h)
              ((char=? #\I ch) #\i)
              ((char=? #\J ch) #\j)
              ((char=? #\K ch) #\k)
              ((char=? #\L ch) #\l)
              ((char=? #\M ch) #\m)
              ((char=? #\N ch) #\n)
              ((char=? #\O ch) #\o)
              ((char=? #\P ch) #\p)
              ((char=? #\Q ch) #\q)
              ((char=? #\R ch) #\r)
              ((char=? #\S ch) #\s)
              ((char=? #\T ch) #\t)
              ((char=? #\U ch) #\u)
              ((char=? #\V ch) #\v)
              ((char=? #\W ch) #\w)
              ((char=? #\X ch) #\x)
              ((char=? #\Y ch) #\y)
              ((char=? #\Z ch) #\z)
              (#t ch)
       )
)

-- 
Russell O'Connor                           roconnor@xxxxxxxxxxxx
    <URL:http://www.undergrad.math.uwaterloo.ca/%7Eroconnor/>
``And truth irreversibly destroys the meaning of its own message''
-- Anindita Dutta, ``The Paradox of Truth, the Truth of Entropy''


 DSSSList info and archive:  http://www.mulberrytech.com/dsssl/dssslist


Current Thread