|
JWhitney Wanderer
Joined: 20 Oct 2006 Posts: 51
|
Posted: Mon Jul 16, 2012 8:16 pm
Capitalizing All Words In A String (%proper?) |
I'm trying to create a function that will capitalize all words (separated by spaces), similar to the %proper function except I want it to capitalize EACH word, not just the first.
Input:
If @proper(@string) is used:
Any way to do this? Seems like it would be somewhat common, but I couldn't find anyone else asking for it by searching forums.. |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4691 Location: Pensacola, FL, USA
|
Posted: Mon Jul 16, 2012 8:30 pm |
#FORALL %words(@var) {#IF (%i != %proper(%i)) {var=%replace(@var, %i, %proper(%i))}}
that should work
might end up with some odd capitalization with really short words unless you give it two passes |
|
_________________ Discord: Shalimarwildcat |
|
|
|
JWhitney Wanderer
Joined: 20 Oct 2006 Posts: 51
|
Posted: Tue Jul 17, 2012 1:13 am |
Hmm I'm trying to put this in a function, so I adjusted the @var calls to %1's like this:
Code: |
#LOCAL $titleproper
#FORALL %word(%1) {#IF (%i != %proper(%i)) {$titleproper = %replace(%1, %i, %proper(%i))}}
#RETURN $titleproper |
but it is only capitalizing the last item in the string, and ignoring the rest. The idea is I want to be able to leave the string itself unaltered, but when using the function like @titleproper(@stringlistname), it will display with proper casing on each word. Any way around this? |
|
|
|
JWhitney Wanderer
Joined: 20 Oct 2006 Posts: 51
|
Posted: Tue Jul 17, 2012 1:19 am |
Found a way around it, like this:
Code: |
#LOCAL $titleproper
$titleproper = %1
#FORALL %word($titleproper) {#IF (%i != %proper(%i)) {$titleproper = %replace($titleproper, %i, %proper(%i))}}
#RETURN $titleproper |
Any way to do the same thing for plain text in a string, rather than a string list? |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4691 Location: Pensacola, FL, USA
|
Posted: Tue Jul 17, 2012 3:04 am |
that should work for strings because it is using the output of the %word function, and not he variable directly
|
|
_________________ Discord: Shalimarwildcat |
|
|
|
MattLofton GURU
Joined: 23 Dec 2000 Posts: 4834 Location: USA
|
Posted: Tue Jul 17, 2012 5:06 am |
%word() is being incorrectly used here. %word() requires two parameters, the source string to work on and the word position you wish to return.
You may wish to convert %word() to %list(), using the space character as the delimiter:
Code: |
#function ProperAllWords {
//TODO: set up the exclusion list of words, leave blank to capitalize all words
$disallowed_words = ""
#loop 1,%numwords(%1) {
$word = %word(%1,%i)
#if ((%i = 1) or (%i = %numwords(%1))) {
//always capitalize the first and last words
$word = %proper($word)
} {
#if (!%ismember($word,$disallowed_words)) {
//only capitalize if it's not a disallowed word
$word = %proper($word)
}
}
$result = %additem($word,$result)
}
#return %replace($result,"|"," ")
} |
|
|
_________________ EDIT: I didn't like my old signature |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4691 Location: Pensacola, FL, USA
|
Posted: Tue Jul 17, 2012 3:55 pm |
for some reason i thought thats what %word did, my bad
|
|
_________________ Discord: Shalimarwildcat |
|
|
|
|
|