AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

String Length - add spaces if shorter than X

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
ryans



Joined: 03 Nov 2009
Posts: 5

PostPosted: Thu Nov 05, 2009 1:11 am    Post subject: String Length - add spaces if shorter than X Reply with quote

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
View user's profile Send private message
TLM



Joined: 21 Aug 2006
Posts: 767
Location: The Shell

PostPosted: Thu Nov 05, 2009 1:56 am    Post subject: Reply with quote

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
View user's profile Send private message
MasterFocus



Joined: 08 Apr 2009
Posts: 882
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Thu Nov 05, 2009 2:24 am    Post subject: Reply with quote

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
View user's profile Send private message
Leef_me



Joined: 08 Apr 2009
Posts: 1158
Location: San Diego, California

PostPosted: Thu Nov 05, 2009 2:35 am    Post subject: Reply with quote

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
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Thu Nov 05, 2009 2:48 am    Post subject: Re: String Length - add spaces if shorter than X Reply with quote

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
View user's profile Send private message
TLM



Joined: 21 Aug 2006
Posts: 767
Location: The Shell

PostPosted: Thu Nov 05, 2009 3:30 am    Post subject: Re: String Length - add spaces if shorter than X Reply with quote

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 Very Happy.
_________________

Off diving into dll functions..
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group