改变变量的特定字符?

Alter a specific character of a variable?

是否可以在 AutoHotkey 中定位变量中的特定字符?在这种情况下,我想 uppercase/lowercase 变量中的第一个字符。

::foo::
InputBox, foo, Foo, Enter foo:
/*
Capitalize first character of %foo% here.
Do whatever.
*/
return

在您的情况下,StringUpper 可以:

stringUpper, foo, foo, T

T代表:

the string will be converted to title case. For example, "GONE with the WIND" would become "Gone With The Wind". "

除此之外,您始终可以使用 StringTrimRight/Left or SubStr() 确定子字符串,更改它们并在之后附加它们:

firstLetter     := subStr(foo, 1, 1)
stringUpper, firstLetter, firstLetter
remainingLetters := subStr(foo, 2, strLen(foo))
foo := firstLetter . remainingLetters
; or, equally:
; foo = %firstLetter%%remainingLetters%

@Blauhirn 解决方案的另一种选择:

MsgBox % foo := Format("{:U}",  SubStr(foo, 1,1)) . subStr(foo, 2, strLen(foo))

这也可以在 RegEx 中相当简单地完成。

::::::已编辑::::::::::

这是一个完整的例子,它在删除连接的情况下使用...

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))
}

正则表达式:

;This is the only example that accounts for Whitespace
var := "   regex is cool!"             
MsgBox % RegExReplace(var, "^(\s*)(.)", "$u2")

NumPut/DllCall:

var := "autohotkey is awesome!"
NumPut(Asc(DllCall("CharUpperA", Str,Chr(NumGet( var,0,"UChar")),Str)), var,0,"UChar")
MsgBox % var