| Author |
Message |
Marco Jongen
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, May 03, 2002 - 06:59 am: |
|
Does anyone have a good solution for removing leading zero's? I have tried the pub.remove service, but this creates the following, with these values: inString: searchString: replaceString: useRegex: inString is +0000123979708900 Result is +123979789 And it is supposed to be +123979708900 Hope anyone can help with this... |
Rob Eamon
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, May 03, 2002 - 11:16 am: |
|
Here's Java code that you can adapt to put into a Java service:
// Remove leading characters from a string. // // @return java.lang.String String with all leading characters specified by paramter 'c' removed // @param s java.lang.String Source string // @param c char Leading characters to remove String trimLeft(String s, char c) { if(s == null) return ""; int len = s.length(); int n = 0; while(n < len && s.charAt(n) == c) ++n; return s.substring(n); }
If your string starts with a sign, you'll need to remove it before calling this service and then put it back after you get the trimmed string. |
Michael Herzog
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, May 03, 2002 - 11:30 am: |
|
If you can remove the sign, you can also just wack this string into an integer: String s_no_sign = "0000123979708900"; Long n = new Long(s_no_sign); |
Rob Eamon
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, May 03, 2002 - 11:37 am: |
|
My previous post with the Java code for trimming leading characters didn't get formatted correctly in the e-mail. Please visit the discussion board to see the full post. |
Rob Eamon
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, May 03, 2002 - 12:11 pm: |
|
Here's a way to do it without needing to strip the leading sign: java.text.DecimalFormat df = new java.text.DecimalFormat(); df.setPositivePrefix("+"); Number n = df.parse("+000012397908900"); return n.toString(); |
Erik Versteijnen
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, May 03, 2002 - 03:53 pm: |
|
Just use 'pub.string:replace': inString: +0000123979708900 searchString: ^\+0+ replaceString: + useRegex: true |
Rob Eamon
Rating: N/A Votes: 0 (Vote!) | | Posted on Friday, May 03, 2002 - 04:31 pm: |
|
I always forget about using regular expressions! Awsome solution! |
viv (Unregistered Guest)
Unregistered guest Rating: N/A Votes: 0 (Vote!) | | Posted on Thursday, September 29, 2005 - 02:22 am: |
|
I tried the reg exp to remove just the trailing zeros and failed, can you suggest how to remove just the trailing '0's |
Puneet Verma
Intermediate Member Username: puneetverma Post Number: 48
Registered: 12-2003 Rating: N/A Votes: 0 (Vote!) | | Posted on Thursday, September 29, 2005 - 04:06 am: |
|
Viv, You need to use the $ after the expression to remove the trailing zeroes. inString: 12345000 searchString: 0+$ replaceString: useRegex: true HTH.. Thanks, Puneet Verma |