cancel
Showing results for 
Search instead for 
Did you mean: 

UDF: Jave command/code needed for splitting / concatenation

Former Member
0 Kudos

Hi,

In my IDOC to File interface, I have a requirement to split and format a data string which I plan to do with a UDF.

Input string would be UPTO 10 characters long.

1. I need to insert '-' exactly after the 4th character of the input string

2. Then I need to remove any leading zeroes from this new string

Example:

Input string: "001234567890"

Output:"12-34567890"

Appreciate if you could let me know of any code snippets to sort this out.

Many thanks

Shirin

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Try this.

A simple one

public String ins(String a,Container container)
{
  //write your code here
if(a.length() > 0)
{
String  c = a.substring(0,4);
String d = a.substring(4,a.length());
String e  = c + "-"+ d;
String f = e.replaceAll("^0*","");
return f;
}
else
{
return a;
}

src->ins-->target

Edited by: malini balasubramaniam on Aug 28, 2008 8:19 AM

Former Member
0 Kudos

Thanks Malini.

Perfect code!!!!!. It worked just the way I wanted. No errors.

Many thanks

Shirin

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

Int len = input.length();

If(len > 4)

{

String a1=input.subSequence(0,4).toString();

a1=a1+"-";

String a2=input.subSequence(4,len).toString();

return a2;

}

else return input;

Former Member
0 Kudos

Hi,

Can you also let me know how to then remove the leading zeroes from this modified string.

Many thanks for the code.

Regards

Shirin

Former Member
0 Kudos

Hi,

For your second question

while(z.startsWith("0")){

y=z.replaceFirst("0","");

}

Former Member
0 Kudos

Hi Ramesh,

Even Im having a similar situation, Suppress leading zeroes and place a decimal before the last two digits.

Input string : 000000552

Output : 5.52

I tried the below code...but No luck ... im getting the error as shown in fig1.

Int len = input.length();

If(len > 7)

{

String a1=input.subSequence(0,7).toString();

a1=a1+".";

String a2=input.subSequence(7,len).toString();

return a2;

}

while(z.startsWith("0"))

{

y=z.replaceFirst("0","");

}

else return input;

fig1:

12:30:49 Start of test

Source code has syntax error: /usr/sap/XD1/DVEBMGS20/j2ee/cluster/server0/./temp/classpath_resolver/Mape6ad98e0745d11dd940e00144f0ed0fa/source/com/sap/xi/tf/_B2P_mm_.java:102: ';' expected { ^ 1 error Source code has syntax error: /usr/sap/XD1/DVEBMGS20/j2ee/cluster/server0/./temp/classpath_resolver/Mape6ad98e0745d11dd940e00144f0ed0fa/source/com/sap/xi/tf/_B2P_mm_.java:102: ';' expected { ^ 1 error

12:30:58 End of test

Thanks

- Ravi

Former Member
0 Kudos

Hi Ravi,

Try this code

Import this java.text.NumberFormat;

NumberFormat testNumberFormat = NumberFormat.getNumberInstance();

// set how many places you want to the right of the decimal.

testNumberFormat.setMinimumFractionDigits( 3 );

testNumberFormat.setMaximumFractionDigits( 3 );

hemant_chahal
Contributor
0 Kudos

Hi

Your error is coming becoz of one extra braces.

check your code and see if the braces are proper for all loops.