Capitalise nth word of string?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 729
Joined: 29 Sep 2013, 22:18

Capitalise nth word of string?

28 Dec 2018, 17:15

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.
oif2003
Posts: 213
Joined: 17 Oct 2018, 11:43
GitHub: oif2003

Re: Capitalise nth word of string?

28 Dec 2018, 18:03

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
Last edited by oif2003 on 29 Dec 2018, 02:01, edited 8 times in total.
swagfag
Posts: 3063
Joined: 11 Jan 2017, 17:59

Re: Capitalise nth word of string?

28 Dec 2018, 19:23

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])
}
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Capitalise nth word of string?

29 Dec 2018, 03:53

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
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
oif2003
Posts: 213
Joined: 17 Oct 2018, 11:43
GitHub: oif2003

Re: Capitalise nth word of string?

29 Dec 2018, 12:37

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)
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Capitalise nth word of string?

29 Dec 2018, 12:57

- 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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
oif2003
Posts: 213
Joined: 17 Oct 2018, 11:43
GitHub: oif2003

Re: Capitalise nth word of string?

29 Dec 2018, 13:44

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")
CyL0N
Posts: 210
Joined: 27 Sep 2018, 09:58

Re: Capitalise nth word of string?

30 Dec 2018, 00:53

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

Return to “Ask For Help”

Who is online

Users browsing this forum: asad41163, Bing [Bot] and 198 guests