AutoHotkey String Manipulation and Text Processing

AutoHotkey String Manipulation and Text Processing featured illustration

πŸ“Ž This article includes 1 downloadable practice file ↓

⏱ 3 min readUpdated 11 November 2026

πŸ“˜ AutoHotkey Beginner Course Β· Lesson 6 of 6

Advertisement
In this article
  1. Basic String Operations in AutoHotkey
  2. String Functions Quick Reference
  3. Intermediate: Clipboard Text Processing
  4. Expert: CSV Processing and File Automation
  5. Regular Expressions in AutoHotkey
  6. Continue learning

AutoHotkey excels at text processing and string manipulation. Whether you need to clean up exported data, reformat phone numbers, extract emails, or automate clipboard workflows, AHK provides powerful built-in string functions that can replace hours of manual text editing.

Basic String Operations in AutoHotkey

; Basic string variables and concatenation
firstName := "Atul"
lastName := "Vij"
fullName := firstName . " " . lastName   ; dot = string concatenation
MsgBox %fullName%                         ; Shows: Atul Vij

; Get string length
myText := "Hello World"
MsgBox % "Length: " . StrLen(myText)     ; Shows: 11

; Convert case
upper := StrUpper("hello world")  ; HELLO WORLD
lower := StrLower("HELLO WORLD")  ; hello world

; Extract part of a string (SubStr)
text := "Invoice-2024-11-001"
year  := SubStr(text, 9, 4)   ; Extract 4 chars from position 9: "2024"
MsgBox Year: %year%

String Functions Quick Reference

FunctionPurposeResult Example
StrLen(str)Get string lengthStrLen("Hello") = 5
SubStr(str, pos, len)Extract substringSubStr("Hello", 2, 3) = "ell"
InStr(str, needle)Find position of textInStr("Hello","ll") = 3
StrReplace(str, find, rep)Find and replace textStrReplace("Hi World","World","AHK") = "Hi AHK"
Trim(str)Remove leading/trailing spacesTrim(" hi ") = "hi"
StrSplit(str, delim)Split string to arrayStrSplit("a,b,c",",")
StrUpper(str)Convert to uppercaseStrUpper("hello") = "HELLO"
StrLower(str)Convert to lowercaseStrLower("HELLO") = "hello"

Intermediate: Clipboard Text Processing

The killer use case for AHK string manipulation is processing whatever text you have selected or copied β€” cleaning data, reformatting it, and pasting it back. This is called a “clipboard hotkey”.

; Clean clipboard: remove double spaces, trim each line
^!Clean::
Send ^a         ; Select all text
Send ^c         ; Copy to clipboard
Sleep 200
text := Clipboard

; Remove double spaces in a loop
While InStr(text, "  ")
    text := StrReplace(text, "  ", " ")

; Process line by line
output := ""
Loop, Parse, text, `n, `r
{
    trimmed := Trim(A_LoopField)
    if (trimmed != "")
        output .= trimmed . "`n"
}
Clipboard := RTrim(output, "`n")
Send ^v
MsgBox Done! Text cleaned.
Return

; Reformat dates: DD/MM/YYYY -> YYYY-MM-DD
^!DateFix::
Send ^c
Sleep 100
text := Clipboard
text := RegExReplace(text, "(d{2})/(d{2})/(d{4})", "$3-$2-$1")
Clipboard := text
Send ^v
Return

Expert: CSV Processing and File Automation

; Read a CSV, capitalize names, uppercase codes, write output
^!ProcessCSV::
FileSelectFile, inputFile, 1,, Select CSV File, CSV Files (*.csv)
if (inputFile = "")
    Return

outputFile := SubStr(inputFile, 1, StrLen(inputFile)-4) . "_processed.csv"
FileDelete %outputFile%

; Write header
FileAppend, Name,Code,Amount`n, %outputFile%

Loop, Read, %inputFile%
{
    if (A_Index = 1)   ; Skip header row
        Continue
    
    row := A_LoopReadLine
    cols := StrSplit(row, ",")
    
    ; Capitalize first letter of name
    name := cols[1]
    name := StrUpper(SubStr(name,1,1)) . SubStr(name,2)
    
    ; Uppercase the code
    code := StrUpper(cols[2])
    
    ; Clean amount (remove $ and spaces)
    amount := StrReplace(cols[3], "$", "")
    amount := Trim(amount)
    
    FileAppend, %name%,%code%,%amount%`n, %outputFile%
}
MsgBox % "Processed " . (A_Index-1) . " rows! Saved to: " . outputFile
Return

Regular Expressions in AutoHotkey

AHK supports full PCRE regular expressions via RegExMatch() and RegExReplace():

Advertisement
; Extract email from clipboard
^!Email::
Send ^c
Sleep 100
RegExMatch(Clipboard, "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}", email)
MsgBox Found email: %email%
Return

; Validate Indian mobile number
phone := "9876543210"
if RegExMatch(phone, "^[6-9]d{9}$")
    MsgBox Valid Indian mobile!
else
    MsgBox Invalid number format

Practical workflow: Select messy text in Excel, Word, or any app, press your hotkey, and AHK reformats it and pastes the cleaned version back β€” all in under a second.

πŸ“Ž Practice files for this article

  • ⌨️
    Lesson 6 script (AutoHotkey v2)Double-click to run after installing AutoHotkey v2.
    ⬇ AHK Β· 780 B

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong