Hallo.
I need to set a string to a defined length; if it is short I need to add charaters to left or right; how is the better solution?
Thank you very much.
Set string to a standard length
Re: Set sting to a standard length
Hallo,
try:
try:
Code: Select all
Str := "abc"
MsgBox,% ">" Str "<`nLen: " StrLen(Str)
Str := Format("{:10}",Str) ;Len = 10
MsgBox,% ">" Str "<`nLen: " StrLen(Str)
Last edited by Rohwedder on 02 Dec 2022, 05:01, edited 1 time in total.
Re: Set sting to a standard length
short example:
Edit: Rohwedder was faster and his use of Format is also smarter
see https://www.autohotkey.com/docs/commands/Format.htm#Examples -> Padding with spaces
Code: Select all
MyString := "abcdef"
StringMinLenght := 10
CharsToAdd := "#"
if ((diff := StringMinLenght - StrLen(MyString)) > 0)
loop % diff
MyString .= CharsToAdd
MsgBox % MyString
see https://www.autohotkey.com/docs/commands/Format.htm#Examples -> Padding with spaces
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: Set string to a standard length
Thank you very much.
All two solution are useful for me.
All two solution are useful for me.
Re: Set string to a standard length
https://biga-ahk.github.io/biga.ahk/#/?id=pad can pad the string to {x} characters, it doesn't truncate however.
- flyingDman
- Posts: 2791
- Joined: 29 Sep 2013, 19:01
Re: Set string to a standard length
Code: Select all
Str := "abcdef"
Min := 10
Chr := "#"
msgbox % substr(Str . StrReplace(Format("{:" Min "}", ""), " ", Chr),1, Min)
14.3 & 1.3.7