| View previous topic :: View next topic |
| Author |
Message |
ryans
Joined: 03 Nov 2009 Posts: 5
|
Posted: Thu Nov 05, 2009 1:11 am Post subject: String Length - add spaces if shorter than X |
|
|
Hello,
Im trying to make a string exactly 12 characters.
trim a string if its over 12 (remove everything after the 12th)
and add spaces to the end if its under 12 to make it 12 long.
StringLeft, partnumber, VAR1, 12
(THIS REMOVES ANY CHARACTERS OVER THE 12TH FROM THE LEFT)
StringLen, partnumberlength, partnumber
(THIS WILL GET ME THE LENGTH OF THE partnumber variable)
How do I write code that will say:
if partnumberlength is less than 12 add spaces to make it 12 characters???
Thanks - Ryan |
|
| Back to top |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 767 Location: The Shell
|
Posted: Thu Nov 05, 2009 1:56 am Post subject: |
|
|
Try this:
| Code: | strDif=
InputBox, partnumber, String,,, 300, 100 ; Only needed for this example.
StringLen, partnumberlength, partnumber
if partnumberlength < 12
{
strDif := 12 - partnumberlength
Loop, % strDif
partnumber .= Chr(32)
}
StringLen, partnumberlength, partnumber ; Only needed for this example.
Msgbox, PartNumber is: %partnumber%`nPartNumberLength is: %partnumberlength% |
This will add the difference in spaces if the partnumber string is less than 12.
It can be easily adapted for your use.
hth _________________
Off diving into dll functions.. |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 882 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Thu Nov 05, 2009 2:24 am Post subject: |
|
|
StrPad()
be sure to use 0 for the "left" parameter _________________
Antonio França
aka MasterFocus aka Tunis
+ My AHK stuff: ~MasterFocus
+ AHK @ irc.freenode.net: #ahk
Contact: PM only ! |
|
| Back to top |
|
 |
Leef_me
Joined: 08 Apr 2009 Posts: 1158 Location: San Diego, California
|
Posted: Thu Nov 05, 2009 2:35 am Post subject: |
|
|
Try the following example in the manual, since you want 12 characters, add 2 more spaces within the quotes,
and add 2 to the number (10+2=12, -9 +2 = -7) :thumbs-up:
http://www.autohotkey.com/docs/commands/SetFormat.htm
see the section: General Remarks
>>To pad an integer with zeros or spaces without having to use floating point math on it, follow this example:
| Code: |
Var := " " . Var ; The quotes contain 10 spaces. To pad with zeros, substitute zeros for the spaces.
StringRight, Var, Var, 10 ; This pads the number in Var with enough spaces to make its total width 10 characters.
Var := SubStr(" " . Var, -9) ; A single-line alternative to the above two lines. |
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 7159
|
Posted: Thu Nov 05, 2009 2:48 am Post subject: Re: String Length - add spaces if shorter than X |
|
|
| ryans wrote: | How do I write code that will say:
if partnumberlength is less than 12 add spaces to make it 12 characters??? |
| Code: | partnumber=skan27
partnumber := SubStr( partnumber . " ", 1, 12 )
MsgBox [ %partnumber% ] |
|
|
| Back to top |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 767 Location: The Shell
|
Posted: Thu Nov 05, 2009 3:30 am Post subject: Re: String Length - add spaces if shorter than X |
|
|
| SKAN wrote: | | Code: | partnumber=skan27
partnumber := SubStr( partnumber . " ", 1, 12 )
MsgBox [ %partnumber% ] |
|
Nice and neat compared to my example and no need for the double StringLen check . _________________
Off diving into dll functions.. |
|
| Back to top |
|
 |
|