cancel
Showing results for 
Search instead for 
Did you mean: 

Substring

Former Member
0 Kudos

I am using a substring function to get the 1st eight char of the string but the mapping fails if the input has no data, how can I handle this?do we have to write a UDF for this?please help

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

You can use this simple UDF which takes a single String array as input , say s[ ] and it has a single ResultList item named result as output.

The complete code is as follows:


public void substrn(String[] s,ResultList result,Container c)
{
    if((s[0]!=null) && (s[0].length()>0))
        {
               result.addValue(s[0].substring(0,8);
        }
   else
              result.addValue("");
}
 

Map the output of this UDF to the target side field.

Thanks

Biswajit

Edited by: 007biswa on Feb 22, 2011 8:45 AM

Edited by: 007biswa on Feb 22, 2011 8:46 AM

Edited by: 007biswa on Feb 22, 2011 8:48 AM

udo_martens
Active Contributor
0 Kudos

Hi,

it makes sense to ask for a string greater 7 (instead of 0)

if((s[0]!=null) && (s[0].length()>7))

Regards,

Udo

Answers (5)

Answers (5)

markangelo_dihiansan
Active Contributor
0 Kudos

Hello,

No need to use UDF. You can try using the mapping below:


source --> mapWithDefault:        --> substring(0,8) --> trim --> target

The source context should be unmodifiend. The value of mapWithDefault contains 8 spaces. The trim will be used to eliminate whitespaces.

Hope this helps,

Mark

Former Member
0 Kudos

Use the standard length function to check if the string length is greater than 8 as well instead of checking for just 0.

Because if your input string is less than 8 then also substring will throw an error.

Regards,

Vishal

deepak_shah
Contributor
0 Kudos

Hi,

You can first check if the field exist using standard node function "exists" and then compare it with empty constant.

If it exists and is not empty then you can use substring function to send 8 characters to target. if its empty you can simply send empty constant to target. No need for UDF here.

Regards,

Deepak

Former Member
0 Kudos

Hi,

There is no need of UDF for this.

You can handle this with a simple logic using equalS text function and "if then else" boolean function.



SourceString       ->           constant "        "(8 spaces) to then        
                      equalS  -> <if  then else>   --->substring(0...8)  -- > Target Field
constant ""(blank) ->            SourceString to else

I think this should satisfy your requirement.

Regards,

Aravind

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>>> if the input has no data, how can I handle this?do we have to write a UDF for this?please help

Solution 1)

Use standard length function and see the value of the string... if the string has length > 0, then do the substring to pull first 8 characters.

you dont need UDF for this simple logic... if you want, you can use the below UDF too...

solution 2)

Example your input string variable in the udf method is inputString, do as follows

String output="";

if (inputString!=null && inputString.length() >0){

output = inputString.substring(0,8);

}

return output;