Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

UDF to replace 0 with spaces

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();
for (int i=0;i<val.length();i++){
if (val.substring(i).startsWith("0")){
result = result + val.substring(i, val.length());
i = val.length();
}catch(Exception e){
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,


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

Former Member
Former Member replied


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.



0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question