Code: Select all
; s = string, n = nth word, delim = delimiters, ignore = ignored chars, i = start pos
capitalizeN(ByRef s, n, delim:=" `t`r`n:;,.!?()=", ignore:="-'`"", i:=1) {
while n
InStr(ignore,c:=SubStr(s,i,1)) || inword:=InStr(delim,c) ? 0 : inword || n--, i++
return SubStr(s,1,i-2) StrUpper(SubStr(s,i-1,1)) SubStr(s,i)
}
s := "
(
"out (of)
africa;;;is
a:'movie'"!.out of africa,is=an ? "africa-themed" movie
)"
loop 13
msgbox(capitalizeN(s, A_Index))
/* equivalent versions
capitalizeN(ByRef s, n, delimiters :=" `t`r`n:;,.!", ignorelist := "-'`"") {
loop parse s
if !InStr(ignorelist, A_LoopField)
if n
inword := InStr(delimiters, A_LoopField) ? 0 : inword || n--
else
return SubStr(s, 1, (p := A_Index - 1) - 1) StrUpper(SubStr(s, p, 1)) SubStr(s, p + 1)
}
capitalizeN(s, n, delimiters :=" `t`r`n:;,.", ignorelist := "-'`"") {
wordCount := inword := 0
loop parse s {
if InStr(ignorelist, A_LoopField) {
continue
} else if wordCount == n {
pos := A_Index - 1
break
} else if InStr(delimiters, A_LoopField) {
inword := 0
} else if !inword {
wordCount++, inword := 1
}
}
return SubStr(s, 1, pos-1) StrUpper(SubStr(s, pos, 1)) SubStr(s, pos+1)
}
*/