Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Report Design
Message Icon Topic: Padding a string to be the same length Post Reply Post New Topic
Author Message
dimsrobert
Newbie
Newbie


Joined: 06 Oct 2008
Location: United States
Online Status: Offline
Posts: 9
Quote dimsrobert Replybullet Topic: Padding a string to be the same length
    Posted: 09 Apr 2009 at 3:22pm
Hi all-
 
I am trying to create a formula for a cross tab that has two data fields in it (both strings) in CR 2008.
 
 
it looks like this;
 
{Vmin.MinAbbr}+'  '+({Vmin.MinName})
 
I have to use a cross tab with both fields in the same formula, if I use two cross tabs, one for each field the MinName drops off of the report because although the MinAbbr is always different the MinName can be the same.
 
Example data is
 
VOCA1 VOCATION
VOCA2 VOCATION
VOCA133 VOCATION
 
As you can see when the length of the MinAbbr changes the lineup of the second column goes wacky.
 
I need to be able to pad the MinAbbr field in the foruma so it's always the same length (say 14 characters) so the second column MinName always lines up left justified as well. Making my data look like this
 
VOCA1        VOCATION
VOCA2        VOCATION
VOCA133    VOCATION
 
I tried using the Space function and several other ways but I can't seem to get it to work.
 
Does this make any sense? Sorry for the long explanation and thanks in advance for any help you might have.
 
Rob


Edited by dimsrobert - 09 Apr 2009 at 3:55pm
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 10 Apr 2009 at 6:12am
Haven't had to use this syntax in a while, but did you try this?
 
{table.MinAbbr} + space(14-len(trim({table.MinAbbr})))
 
Just wondering, this should add spaces to the end of the string
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 10 Apr 2009 at 7:35am
Lockwelle was definately on the right track. Although an easy solution might be to just right justify it so it lines up nicely that direction     Smile
 
Reworking lockwelle's code just a bit should insert the correct # of spaces and add the last part on:
trim({Vmin.MinAbbr}) +
space(20-(len(trim({Vmin.MinAbbr})+ trim({Vmin.MinName}))))
+ trim({Vmin.MinName})
Then you will most likely have to adjust the "Character spacing exactly" in the Field Format Editor, Font tab to about 5 or so to make these line up.
You can change the value of "20" in the formula to whatever gives you a nice spacing between the 2 items.


Edited by DBlank - 10 Apr 2009 at 7:36am
IP IP Logged
varadharaj.ms
Newbie
Newbie


Joined: 27 Jun 2010
Online Status: Offline
Posts: 1
Quote varadharaj.ms Replybullet Posted: 27 Jun 2010 at 11:13pm
Dears,
 
You could make use of this custom function for any char replacing [ Similar to PadLeft/PadRight in .Net String functions]..
// Function Name : cfnPad()
Function (stringvar PadType,stringvar PadString,stringvar PadChar,numbervar CharCount)
stringvar rStr:=trim(PadString);
numbervar lStr:=len(rStr);
numbervar i:=0;
if UpperCase(PadType)='RIGHT' then
    for i := 1 to CharCount-lStr do
    (
        rStr := rStr + PadChar ;
    )
else if UpperCase(PadType)='LEFT' then
    for i := 1 to CharCount-lStr do
    (
        rStr := PadChar  + rStr;
    );
rStr
//..
 
Implemet:  cfnPad('RIGHT','234','0',5)
 
Results as : 23400
 
Folks ... Let me know for any
 
Varadh.
 
 
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.031 seconds.