cancel
Showing results for 
Search instead for 
Did you mean: 

Insert character at a special position in a string.

Former Member
0 Kudos

Hi,

What is the best way to insert a character at a special position in a string?

I have a string, i.e 123456789 (the length can vary) and I want to insert a "-" before the last three digits. The result should be 123456-789.

Is it possible to do this with standard functions?

Thanks for your help.

/Sten

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

HI,

Its not possible with existing Standard APIs in Mapping.

You need to go for UDf for this kind of requirment. Just one statement code will be fine

UDF Code

Cache Parameter -- Value

A--->TestUDF -


>B

//Write your code

return(A.substring(0,A.length()-3)"-"A.substring(A.length()-3,A.length()));

Thanks

Swarup

Answers (2)

Answers (2)

GabrielSagaya
Active Contributor
0 Kudos

function myudf(String a, Container container)

{

int len=a.length();

char[[]] chars = str.toCharArray;

char[[]] chart = new char[[len+1]];

for(int i=0;i<len-3;i++)

chart[<i>]=chars[<i>];

chart[<i>]='-';

for(int j=len-3;j<len;j++)

{

i++;

chart[<i>]=chars[[j]];

}

String mystring = new String(chart);

return mystring;

}

Former Member
0 Kudos

Hi

if this is ur input then u can do using std function.

though it is length method but will solve ur problem.

1) Use substring fucntion which will take 123456 value.

2) Use constant function with - value.

3) Use concat function and give 1 and 2 input.

4) Use substring function which will take 789 value

5) Use concat function and give 3 and 4 input.

chirag.

Former Member
0 Kudos

Hi Chirag,

The solution given by you is precise if the length is fixed, I think its varying as per the User post....so have to go for UDF...

Thanks

Swarup

Former Member
0 Kudos

Hi Swarup,

Yes u r correct the solution which i have provided will work only for fixed length.

I think he has to go with the UDF which u ahve provided.

Cheers,

chirag