Page 1 of 1

Capitalise nth word of string?

Posted: 28 Dec 2018, 17:15
by PuzzledGreatly
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?

Posted: 28 Dec 2018, 18:03
by oif2003
Edit: Sorry I miss read it. This is not exactly what you asked for but this is what I ended up with.

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)
v2:
Spoiler

Re: Capitalise nth word of string?

Posted: 28 Dec 2018, 19:23
by swagfag
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?

Posted: 29 Dec 2018, 03:53
by jeeswg
Here's a way. Cheers.

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
[EDIT:] Changed the second * to +.

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

Re: Capitalise nth word of string?

Posted: 29 Dec 2018, 12:37
by oif2003
jeeswg wrote:
29 Dec 2018, 03:53
Nifty! :bravo:

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?

Posted: 29 Dec 2018, 12:57
by jeeswg
- Haha cheers.
- 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

Re: Capitalise nth word of string?

Posted: 29 Dec 2018, 13:44
by oif2003
jeeswg wrote:
29 Dec 2018, 12:57
Thanks! :D 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?

Posted: 30 Dec 2018, 00:53
by CyL0N
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}")
}

Re: Capitalise nth word of string?

Posted: 19 Sep 2022, 01:25
by Chunjee
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?

Posted: 19 Sep 2022, 05:24
by BoBo
without substr() :mrgreen:

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
   }