AnsweredAssumed Answered

Using Groovy to remove preceding zero not working as expected

Question asked by faiza.naz365123 on May 30, 2017
Latest reply on May 31, 2017 by 51662476


I have a requirement to remove leading zeros from a csv specific column using Groovy script. I am using this groovy but this is not working as expected nor giving any result. Can someone please help me what I am doing wrong.


Input CSV:,Test User,0000100098,8797784047620,,,,0,2016-01-18 11:36:32.847,0000123652,Test2 User,0000100098,87976289823652,,,,0,2016-01-18 11:35:45.327,000012352


I have to remove leading zeros from 3rd column "0000100098" and make it "100098" before i pass this data to my map.


import java.util.Properties;

String DELIMITER = ",";

for( int i = 0; i < dataContext.getDataCount(); i++ ) {

InputStream is = dataContext.getStream(i);
Properties props = dataContext.getProperties(i);

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer outData = new StringBuffer();
int lineNum = 0;

while ((line = reader.readLine()) != null) {
// Parse the line into separate columns splitting on the delimiter defined above
String[] columns = line.split(DELIMITER);

// Obtain a column's value by its known position on the line (zero-indexed)
String someColumnValue = columns[2];

columns[2] = someColumnValue.replaceFirst("^0+(?!\$)", "");

//String AccountId = someColumnValue.replaceFirst("^0+(?!\$)", "");
//columns[2] = "Faiza";
//columns[2] = AccountId;

// Add line to output buffer including a line break at the end


// Convert the output StringBuffer to an InputStream and store in the dataContext
is = new ByteArrayInputStream(outData.toString().getBytes("UTF-8"));
dataContext.storeStream(is, props);