Capitalise first letter on multiple optional strings

Get help with using AutoHotkey and its commands and hotkeys
Plus200
Posts: 8
Joined: 14 Oct 2013, 20:19

Capitalise first letter on multiple optional strings

06 Jun 2016, 16:09

I want to capitalise first letter and set the rest to lower case for a very long list of words (countries and names).

Is there a RegEx way to do that?

I'm moving back to AHK from Phrase Express after 3 years mainly because they don't support RegEx, but I'm not sure how to use RegEx.

Thanks!
User avatar
Xtra
Posts: 1833
Joined: 02 Oct 2015, 12:15

Re: Capitalise first letter on multiple optional strings

06 Jun 2016, 16:22

Example:

Code: Select all

text := "this is sOme rANdom text and soMe more text"

StringUpper, output, text, T

MsgBox % output
Plus200
Posts: 8
Joined: 14 Oct 2013, 20:19

Re: Capitalise first letter on multiple optional strings

06 Jun 2016, 16:32

Thanks, but… how do I use it in the .ahk file?

Say that I want to capitalise all the following:
europe
i
steve
seattle
james

What should I add to the ahk file? In RegEx, if possible.

Thanks again.
User avatar
Xtra
Posts: 1833
Joined: 02 Oct 2015, 12:15

Re: Capitalise first letter on multiple optional strings

06 Jun 2016, 23:12

Code: Select all

inputtext := "Hi my name is steve! i live in seattle and my friend james lives in europe."

inputtext := StrReplace(inputtext, "europe", "Europe")
inputtext := StrReplace(inputtext, " i ", " I ")
inputtext := StrReplace(inputtext, "steve", "Steve")
inputtext := StrReplace(inputtext, "seattle", "Seattle")
inputtext := StrReplace(inputtext, "james", "James")

MsgBox % inputtext
HTH
florisvos
Posts: 20
Joined: 15 Apr 2016, 06:26
Facebook: https://www.facebook.com/floris.vos.77

Re: Capitalise first letter on multiple optional strings

06 Jun 2016, 23:23

I made some code that works.

Code: Select all

array := [0,0] ; make the array
Loop, Read, C:\Users\f-vos\Desktop\a.txt ; path to .txt file which has 1 word on each line
{
firstletter:=substr(a_loopreadline, 1, 1) ; get first letter
StringUpper, first, firstletter ; capitalise first letter 
Stringright, rest, a_loopreadline, strlen(a_loopreadline)-1 ; get the rest of the word
StringLower, rest, rest ; make it lowercase
array[a_index] := first ; append it to the array
array[a_index] .= rest ; same 
}
loop % array.length()
msgbox % array[a_index]
Update: You could throw in an if-check, to loop through a file word by word and then use the above code to see if the word is in a list of words that has to be capitalised, and if it is, capitalize it.
Plus200
Posts: 8
Joined: 14 Oct 2013, 20:19

Re: Capitalise first letter on multiple optional strings

07 Jun 2016, 12:34

I get the error below.

Can someone guide me step by step how to incorporate the code in the ahk file I already have?

Thanks again!

2016-06-07 10.33.14-Capitalise first letter on multiple optional strings - AutoHotkey Community.png
2016-06-07 10.33.14-Capitalise first letter on multiple optional strings - AutoHotkey Community.png (11.96 KiB) Viewed 1826 times
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Capitalise first letter on multiple optional strings

08 Jun 2016, 09:41

Plus200 wrote:I get the error below.

Can someone guide me step by step how to incorporate the code in the ahk file I already have?

Thanks again!

2016-06-07 10.33.14-Capitalise first letter on multiple optional strings - AutoHotkey Community.png
Sounds like you are using an old version of AutoHotkey.
User avatar
sinkfaze
Posts: 614
Joined: 01 Oct 2013, 08:01

Re: Capitalise first letter on multiple optional strings

08 Jun 2016, 09:55

I can't remember a time when StringUpper wasn't in the language. Are you using v2?
florisvos
Posts: 20
Joined: 15 Apr 2016, 06:26
Facebook: https://www.facebook.com/floris.vos.77

Re: Capitalise first letter on multiple optional strings

08 Jun 2016, 13:37

Update of my script with info from other post:

Code: Select all

array := [0,0] ; make the array
Loop, Read, C:\Users\f-vos\Desktop\a.txt ; path to .txt file which has 1 word on each line
{
StringUpper, word, a_loopreadline, t ; capitalise first letter and make the rest small
array[a_index] := word 
}
loop % array.length()
msgbox % array[a_index]
Just get a more recent version and it'll work :)
If there's multiple words on each line you can use a Parse loop

Return to “Ask For Help”

Who is online

Users browsing this forum: RyConnMD, scriptor2016, tatagi and 40 guests