Print Page | Close Window

Padding a string to be the same length

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Report Design
Forum Discription: The best way to design a report and problems you have encountered
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=6042
Printed Date: 06 May 2024 at 5:44pm


Topic: Padding a string to be the same length
Posted By: dimsrobert
Subject: Padding a string to be the same length
Date 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



Replies:
Posted By: lockwelle
Date 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


Posted By: DBlank
Date 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.


Posted By: varadharaj.ms
Date 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.
 
 



Print Page | Close Window