Capitalise nth word of string?
- PuzzledGreatly
- Posts: 1303
- Joined: 29 Sep 2013, 22:18
Capitalise nth word of string?
I made a function to capitalise the nth word of a string and it works but is bulky and I just wondered if there was a single line of RegExReplace that could do the same thing. Eg change, "out of africa is a movie " to "out of Africa is a movie". Thanks.
Re: Capitalise nth word of string?
Edit: Sorry I miss read it. This is not exactly what you asked for but this is what I ended up with.
v1:
v2:
v1:
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++
StringUpper c, % SubStr(s,i-1,1)
return SubStr(s,1,i-2) c 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)
Spoiler
Last edited by oif2003 on 29 Dec 2018, 02:01, edited 8 times in total.
Re: Capitalise nth word of string?
define word
Code: Select all
str = out of africa is an "africa-themed" movie
MsgBox % capitaliseNthWord(str, 6)
capitaliseNthWord(str, n) {
static NEEDLE := "(?:[[:alpha:]]|-)+"
WordPositions := []
Loop
{
if (A_Index == 1)
pos := RegExMatch(str, NEEDLE, match)
else
pos := RegExMatch(str, NEEDLE, match, pos + StrLen(match))
if !pos
break
if match
WordPositions.Push(pos)
else
++pos
}
return RegExReplace(str, "(.)", "$U1", , 1, WordPositions[n])
}
Re: Capitalise nth word of string?
Here's a way. Cheers.
[EDIT:] Changed the second * to +.
Code: Select all
q:: ;capitalise first letter of nth word
vText := "out of africa is a movie"
vNum := 3
MsgBox, % RegExReplace(vText, "^([^ ]* *){" (vNum-1) "}\K.", "$U0")
return
Code: Select all
q:: ;capitalise first letter of nth word
vText := "out of africa is a movie"
vOutput := ""
Loop, 8
{
vNum := A_Index
;vOutput .= RegExReplace(vText, "^([^ ]* *){" (vNum-1) "}(\x22?)\K.", "$U0") "`r`n" ;not quite right
vOutput .= RegExReplace(vText, "^([^ ]* +){" (vNum-1) "}(\x22?)\K.", "$U0") "`r`n"
}
MsgBox, % vOutput
return
Last edited by jeeswg on 29 Dec 2018, 12:59, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Capitalise nth word of string?
Nifty!
I have trouble getting the correct results when the phrase starts with quotations so I inserted "`n" before the string and then later removed it. How can we do this more cleanly?
Code: Select all
s =
(
firstword
"out (of)
africa;;;is
a:'movie'"!.out of africa,is=an ? "africa-themed" movie
)"
loop 14
msgbox % SubStr(RegExReplace("`n" s, "^([\w\-]*[\W]*){" A_Index "}\K.", "$U0"), 2)
Re: Capitalise nth word of string?
- Haha cheers.
- Like this perhaps?
- Like this perhaps?
Code: Select all
q:: ;capitalise first letter of nth word (+ ignore leading quote)
;vText := "out of africa is a movie"
vText := Chr(34) "out of africa is a movie" Chr(34)
vOutput := ""
Loop, 8
{
vNum := A_Index
vOutput .= RegExReplace(vText, "^([^ ]* +){" (vNum-1) "}(\x22?)\K.", "$U0") "`r`n"
}
MsgBox, % vOutput
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Capitalise nth word of string?
Thanks! I'll check that out later. For now this seems to work for me:
Code: Select all
RegExReplace(s, "^([\w\-]*[\W]*){" n-1 "}\K[\w\-]*", "$t0")
Re: Capitalise nth word of string?
I see a lot of variations, i think this covers them all.
Code: Select all
str = out of africa is an "africa-themed" movie
MsgBox % modifyNthWord(str,6,"U") "`n"
. modifyNthWord(str,6,"T") "`n"
. modifyNthWord(str,6,"L")
modifyNthWord(str, n,doWhatToWord:="U",wordDelim:=" "){ ;doWhatToWord can be U,L or T. i.e Upper,Lower,Titlecase
Return % RegExReplace(StrSplit(str,wordDelim)[n], "(.*)", "$" doWhatToWord "{1}")
}
live ? long & prosper : regards
Re: Capitalise nth word of string?
with substr:
Code: Select all
var := "out of africa is a movie "
n := 8
msgbox, % subStr(var, 1, n-1) format("{1:U}", (subStr(var, n, 1))) subStr(var, n+1)
; => "out of Africa is a movie "
Re: Capitalise nth word of string?
without substr()
Code: Select all
n:=3
str:=StrSplit("out of africa is a movie ", A_Space)
MsgBox % res := StrReplace(str, str[n], StrTitle(str[n]))
StrTitle(inVar) {
StringUpper, outVar, inVar, T
return outVar
}
Who is online
Users browsing this forum: No registered users and 234 guests