Capitalise nth word of string?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
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: 214
Joined: 17 Oct 2018, 11:43
Contact:

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: 6222
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: 6902
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: 214
Joined: 17 Oct 2018, 11:43
Contact:

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: 6902
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: 214
Joined: 17 Oct 2018, 11:43
Contact:

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: 211
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
User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: Capitalise nth word of string?

19 Sep 2022, 01:25

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 "
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Capitalise nth word of string?

19 Sep 2022, 05:24

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
   }

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 115 guests