AutoHotkey Community

It is currently May 26th, 2012, 11:11 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: November 5th, 2009, 2:11 am 
Offline

Joined: November 3rd, 2009, 8:19 pm
Posts: 5
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 2:56 am 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
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

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 3:24 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
StrPad()
be sure to use 0 for the "left" parameter

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 3:35 am 
Online

Joined: April 8th, 2009, 7:49 pm
Posts: 6067
Location: San Diego, California
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.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 5th, 2009, 3:48 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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% ]


Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 5th, 2009, 4:30 am 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
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 :D.

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: joetazz, JSLover, Maestr0, rbrtryn, Tipsy3000, WillTroll and 62 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group