[racket-users] Re: Naive question on how to capitalize only the first word of a string

2017-06-19 Thread Jack Firth
Given how many solutions everyone's giving, this would be a good rosetta code 
task :)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Re: Naive question on how to capitalize only the first word of a string

2017-06-19 Thread Jack Firth
On Monday, June 19, 2017 at 5:12:10 PM UTC-7, Glenn Hoetker wrote:
> I'm quite new to Racket/LISP, so I hope this isn't breathtakingly obvious.  
> Can someone please tell me the best way to capitalize just the first word in 
> a multiword string.  So, given the string "it was a dark and stormy night", I 
> would like to get "It was a dark and stormy night". I see functions for 
> turning everything lower case, everything uppercase or capitalizing each 
> word, but nothing in line with what I hope to do.
> 
> > (define it "cat dog")
> > (string-titlecase it)
> "Dog Cat"  ; So close, but not quite "Dog cat" as I want.
> 
> Many thanks.

You'll want to split the string into a list of words and apply the 
capitalization function to only the first word.

(define (capitalize-first sentence-str)
  (string-join (list-update (string-split sentence-str) 0 string-titlecase)))

Disclaimer: this will do some weird things to the whitespace in sentence-str 
(e.g. convert newlines to spaces); you might want to tweak that.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.