cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to replace 0 with spaces

Former Member
0 Kudos

Hi experts,

i am having trouble designing an UDF to replace 0 with spaces until an alpha carater is reached. to be more precise :

I have for example as input '000S9002', I would like to have as output ' S9002' (3 white spaces before S)

other example : '0000P128900' => ' P128900'.(4 white spaces before P)

I would like to replace all the zeros before the alpha caracter with spaces.

So far I made :


public String test(String val,Container container){

String result = " ";
val = val.trim();
		
try{
	
for (int i=0;i<val.length();i++){
if (val.substring(i).startsWith("0")){
				
	}
else{
				
result = result + val.substring(i, val.length());
i = val.length();
}
}
		
}catch(Exception e){
e.printStackTrace();
}
		
		
return result;
	}
}

So far I can manage to add only one space instead of all the zeros.

'0000P128900' => ' P128900'. (only 1 white space before P instead of 4)

Any help would be greatly appreciated.

Kind regards,

Jamal

Edited by: ba_da_boom on Feb 5, 2009 5:19 PM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

I guess all you need is to insert another for loop inside of the else statement:

> else{

> result = result + val.substring(i, val.length());

> for (int index = 1; index < i; index++) {

> result = " " + result;

> }

> i = val.length();

> }

index = 1, because you already added one space.

Hope it works.

Regards

Patrick

Answers (1)

Answers (1)

Former Member
0 Kudos

Patrick, always there when I need help )

THANKS again

Kindly yours,

Jamal