I would like to bring words with something in front of them to the front.
For example, turn bla bla foobla into foobla bla bla - put the word with foo in the front.
This would be quite easy with another language with stronger string manipulation functions, however I need to use AutoHotKey for functions that it provides.
Right now, I'm thinking of splitting the string into words (split by ' '), but I'm not even sure if I can find out the length of an 'array' in AHK.
Is doing this even possible in AHK?
string := "bla bla fooble"
arr := StrSplit(string, " ")
msgbox % "the number of elements: " arr.maxindex()
removed := arr.remove(arr.maxindex())
arr.insert(1, removed)
For k, v in arr {
output .= k ": " v "`n"
}
msgbox % output
StrSplit(ByRef InputVar, Delimiters="", OmitChars="") {
o := []
Loop, Parse, InputVar, % Delimiters, % OmitChars
o.Insert(A_LoopField)
return o
}
Related
Lets say my Cliboard variable is currently in this state:
Clipboard:= "-Classes Essay on Hobbes
I want to perform If conditions depending on the first block/word in the clipboard, in this case -Classes . Essentially doing something like below using FileAppend:
if "-diary"{
;Create file in /Diary/Entry 27.MD
}
Else if "-Classes"{
;Create file in /Classes/Essay on Hobbes.MD
}
I was able to find this page in the docs If Var in/contains MatchList - Syntax & Usage | AutoHotkey but its not positional, which means if the block -Classes is also present as part of the file name for some reason, my logic breaks.
I have taken the liberty to crosspost this question on other forums as well.
Any help would be greatly appreciated!
Or.. you can just:
Clipboard:= "-Classes Essay on Hobbes"
Gosub % subroutine := SubStr( Clipboard, 1, InStr( Clipboard, " " )-1 )
Clipboard:= "-diary diary example"
Gosub % subroutine := SubStr( Clipboard, 1, InStr( Clipboard, " " )-1 )
Return
-diary:
MsgBox % "Diary:`n" StrReplace( clipboard, subroutine " " )
;Create file in /Diary/Entry 27.MD
Return
-Classes:
MsgBox % "Classes:`n" StrReplace( clipboard, subroutine " " )
;Create file in /Classes/Essay on Hobbes.MD
Return
You might try StrSplit()
; Array := StrSplit(String , Delimiters, OmitChars, MaxParts)
clip_array := StrSplit(Clipboard, A_Space, , 1)
if clip_array[1] = "-diary"{
;Create file in /Diary/Entry 27.MD
}
Else if clip_array[1] = "-Classes"{
;Create file in /Classes/Essay on Hobbes.MD
}
Else {
MsgBox % "No match for " clip_array[1]
}
Described here https://www.autohotkey.com/docs/commands/StrSplit.htm and note the param for MaxParts which addresses how many "tokens" to look in to (keeping the rest in the last) if the clipboard is sufficiently large to matter (in which case I'm not sure any of the other answers address any better).
If the amount of text on the clipboard is enormous, that could cause lagging with the script loading all of that text into the script memory, just to check the first value. I'd recommend regexMatch since that can return a position and capture the word as well without parsing everything.
; for reference of regexMatch function
; regexMatch(Haystack, Needle, outputVar, startPos)
Fnd := regexMatch(Clipboard, "U)(\-[\w]+))", capSubStr, inStr(Clipboard, "-"))
if (Fnd==0) {
Msgbox, Didn't find a word on the Clipboard.
Return
}
checkVal := capSubStr1
if (checkVal=="-diary") {
;Create file in /Diary/Entry 27.MD
} Else if (checkVal=="-Classes") {
;Create file in /Classes/Essay on Hobbes.MD
}
Here is the basic reference for using regular expressions with AHK: https://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm
That will give an idea as to what the above expression is actually looking for, if you are not already familiar with regex.
I have a autohotkeyscript with:
IfInString, pp_text, %A_Space%
{
pp_text := %pp_text%
}
So in case %pp_text% contains a space I want to add " at the beginning and end
Example:
pp_text = Hello World
should then become
pp_text = "Hello World"
How can I do this
You escape a quote by by putting another quote next to it and you concatenate with the concatenate operator ., but actually you can also just omit the operator 99% of the time.
Other things to fix in your script:
Get rid of that super deprecated legacy command and use InStr() instead.
And when you're in an expression, you refer to a variable by just typing its name. Those double % you're using are the legacy way of referring to a variable.
So it's correct in the legacy command, but not in the modern := assignment.
And also you can omit brackets on one-liner if-statements. But that's of course just going to be down personal preference.
Full script:
If (InStr(pp_text, A_Space))
pp_text := """" pp_text """"
Four quotes since the the outer two specify that we're typing a string.
Variable names in an expression are not enclosed in percent signs.
Consequently, literal strings must be enclosed in double quotes to
distinguish them from variables.
To include an actual quote-character
inside a literal string, specify two consecutive quotes as shown twice
in this example: "She said, ""An apple a day.""".
pp_text := "Hello World"
If InStr(pp_text, " ")
pp_text := """Hello World"""
MsgBox % pp_text
EDIT:
To use the name of the variable (not its literal text) in the output expression, you need four quotes, as the user 0x464e explained.
pp_text := "Hello World"
If InStr(pp_text, " ")
pp_text := """" pp_text """"
MsgBox % pp_text
I want a hotkey or hotstring (whatever is easier), so I can easily convert e.g.
1:5 into [1,2,3,4,5] or
3:7 into [3,4,5,6,7] etc..
I want this to work for all integers...
So I want "multiple variants of the same hotstring" (or, if easier: a hotkey that works somewhat similar: e.g. pressing strg + h and typing 1:3 should produces [1,2,3] )
It should recognize that I typed a number followed by colon followed by another number, and then expand correspondingly..
I looked into the Input function, but it does not seem to be exactly what I want..
I don't need a working solution. Hints & links or keywords for further googling are already helpful..
After typing +h or pressing strg+h, type two numbers to produce the desired outcome:
:*:+h::
^h::
nr := "" ; empty variable's content
end_nr := ""
Input, var, L2 ; Length limit=2
; Input, var, L2 V ; V: Visible
If var is not integer
{
MsgBox, "%var%" is not integer
return
}
first_nr := SubStr(var, 1, 1)
second_nr := SubStr(var, 0)
if (first_nr >= second_nr)
{
MsgBox, "%first_nr%" is greater or equal "%second_nr%"
return
}
Loop
{
nr++ ; increase the number in the variable "nr" by 1 in each iteration
if (nr < first_nr)
continue
If (nr = second_nr)
break
end_nr .= nr . "," ; concatenate the outputs by adding a comma to each one
}
If (first_nr = 0)
MsgBox, "0,%end_nr%%second_nr%"
else
MsgBox, "%end_nr%%second_nr%"
return
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
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%
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
I am using Autohotkey.
I have a string that looks like this S523.WW.E.SIMA. I want to remove the last few characters of the string after the dot (including the dot itself). So, after the removal, the string will look like S523.WW.E.
This may look like a simple question but I just cannot figure out using the available string functions in Autohotkey. How can this be done using Autohotkey? Thank you very much.
Example 1 (last index of)
string := "S523.WW.E.SIMA"
LastDotPos := InStr(string,".",0,0) ; get position of last occurrence of "."
result := SubStr(string,1,LastDotPos-1) ; get substring from start to last dot
MsgBox %result% ; display result
See InStr
See SubStr
Example 2 (StrSplit)
; Split it into the dot-separated parts,
; then join them again excluding the last part
parts := StrSplit(string, ".")
result := ""
Loop % parts.MaxIndex() - 1
{
if(StrLen(result)) {
result .= "."
}
result .= parts[A_Index]
}
Example 3 (RegExMatch)
; Extract everything up until the last dot
RegExMatch(string, "(.*)\.", result)
msgbox % result1
Example 4 (RegExReplace)
; RegExReplace to remove everything, starting with the last dot
result := RegExReplace(string, "\.[^\.]+$", "")