Friday, July 10, 2009

trimString method in X++

public static str trimString(str _str, container _trimStr = [" "]){
int counter;
str lTrim = _str;
str rTrim;
//LTrim
for(counter = strlen(_str); counter >= 1 ; counter--)
{
if(confind(_trimStr, strdel(lTrim, 1, counter - 1))) {
lTrim = strdel(lTrim, counter, strlen(lTrim));
}
else
{
break;
}
}

//RTrim
rTrim = lTrim;
for(counter = 1; counter <= strLen(lTrim) ; counter++)
{
if(confind(_trimStr, strdel(rTrim, 2, strlen(rTrim)))) {
rTrim = strdel(rTrim, 1, 1);
}
else
{
break;
}
}
return rTrim;
}

Usage: info("trimString( "0000000000789798798.0000000", ["0", "."] ) -> " + trimString("0000000000789798798.0000000", ["0", "."] ) );

2 comments: