Reputation:
Is it possible to target a specific character in a variable in AutoHotkey? In this case I'd like to uppercase/lowercase the first character in the variable.
::foo::
InputBox, foo, Foo, Enter foo:
/*
Capitalize first character of %foo% here.
Do whatever.
*/
return
Upvotes: 0
Views: 208
Reputation: 2672
Alternative one liner to @Blauhirn's solution:
MsgBox % foo := Format("{:U}", SubStr(foo, 1,1)) . subStr(foo, 2, strLen(foo))
This can also be done in RegEx fairly simply.
::::::Edited::::::::::
Here's a full Example of it's use with the concatenate removed...
fu := "fu"
bar := "beyond all recognition!"
MsgBox % capitalize(fu) A_space capitalize(bar)
Return
capitalize(x) {
return Format("{:U}", SubStr(x, 1,1)) subStr(x, 2, strLen(x))
}
Regular Expression:
;This is the only example that accounts for Whitespace
var := " regex is cool!"
MsgBox % RegExReplace(var, "^(\s*)(.)", "$1$u2")
NumPut/DllCall:
var := "autohotkey is awesome!"
NumPut(Asc(DllCall("CharUpperA", Str,Chr(NumGet( var,0,"UChar")),Str)), var,0,"UChar")
MsgBox % var
Upvotes: 2
Reputation: 10852
In your case, StringUpper will do:
stringUpper, foo, foo, T
T
stands for:
the string will be converted to title case. For example, "GONE with the WIND" would become "Gone With The Wind". "
Other than that, you can always determine substrings using either StringTrimRight/Left or SubStr(), alter them and append them afterwards:
firstLetter := subStr(foo, 1, 1)
stringUpper, firstLetter, firstLetter
remainingLetters := subStr(foo, 2, strLen(foo))
foo := firstLetter . remainingLetters
; or, equally:
; foo = %firstLetter%%remainingLetters%
Upvotes: 2