cancel
Showing results for 
Search instead for 
Did you mean: 

Deleting leading substring from a string

MarkusKlein
Active Contributor
0 Kudos

Hey guys,

how can i delete a string from a leading substring?

e.g.: "00022521" should become "22521".

thx

Markus

Accepted Solutions (1)

Accepted Solutions (1)

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

I actually had written a little static class to handle just this. Here is the static class.




public class LeadingZeroRemover {

	public static String removeLeadingZeros(String str) {
		if (str == null) {
			return null;
		}
		char[] chars = str.toCharArray();
		int index = 0;
		for (; index < str.length(); index++) {
			if (chars[index] != '0') {
				break;
			}
		}
		return (index == 0) ? str : str.substring(index);
	}
}

Then you can simply use it like this.

<b>LeadingZeroRemover.removeLeadingZeros</b> (soldToNumber.getValue().toString())

Regards,

Rich Heilman

roberto_tagliento
Active Contributor
0 Kudos

String STR = new String("00022521");

while ( STR.charAt(0) == '0' )

STR = STR.substring(1);

Answers (2)

Answers (2)

Former Member
0 Kudos

If you care ONLY about leading ZEROES than:

new Integer("0000222521").toString();

Former Member
0 Kudos

Hi Markus,

String string1 = "00022521";

String string2 =string1.substring(3,string1.length);

Hope it helps,

Kind Regards,

Sergio