Ask a Question

how to add leading zero to a string or integer using groovy script

SOLVED
JayKondra
New Contributor

how to add leading zero to a string or integer using groovy script

how to add leading zero to a string or integer using  groovy script

 

a = 12345

 

Result should be 0000012345 (Len = 10)

4 REPLIES 4
richie
Community Hero

Hi 
 
I'm not a groovy scripter - i'd like to be, but what i need is a absolutely no initial coding skills groovy course, which doesnt exist - so I tend to just steal snippets of what other people have given me and try and expand those with help - but isn't what you want just a simple concatenation?
 
Just considering how datatypes work - you wouldn't be able to maintain any leading zeros on an integer I wouldn't have thought cos the zero's would get dropped as 0000012345 = 12345 if numeric - so the values would have to be treated as a string.
 
my ridiculously poor coding skills have come up with the following:
 
a = "12345";
b = "00000";

log.info(b+a);
When I executed the above - the response was as follows:  
 
Sun Sep 23 12:12:21 BST 2018: INFO: 0000012345
 
Is this what you wanted? or have I missed something essential again?
 
Cheers,
 
richie
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Lucian
Community Hero

hey,

check at https://community.smartbear.com/t5/SoapUI-Pro/zero-pad-string-in-groovy-script/m-p/162602#M36569


If my answer helped please click on the 'Accept as Solution' button.
HimanshuTayal
Community Hero

Hi @JayKondra

 

The Solution @Lucian has provided is the quickest and simple but it you want to do an elaborated solution, then you can try below code :

def a = 1234
def stringLen = 10
def strLen = a.toString().size()
expectedLen = stringLen.toInteger() - strLen.toInteger()
for(i = 0; i<=expectedLen-1;i++)
{
	a = "0" + a 
}

log.info a
log.info a.length()

Click "Accept as Solution" if my answer has helped, and remember to give "kudos" 🙂

 

Thanks and Regards,

Himanshu Tayal


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Radford
Super Contributor

If you have an integer value, the Sting.format method linked by @Lucian is ideal, but regarding the other half of your question of padding strings...

 

Groovy strings have the several padding methods included:

  • padRight
  • padLeft
  • center

So in your example, if the variable "a" was a string, you could do the following:

 

def a = "12345"
def zeroPaddedString = a.padLeft(10, "0")
log.info(zeroPaddedString)

The following link explains the padding functions in more details:

 

http://mrhaki.blogspot.com/2009/09/groovy-goodness-padding-strings.html

cancel
Showing results for 
Search instead for 
Did you mean: