cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple values in context concat to one string

Former Member
0 Kudos

Hi,

I need a UDF that concats the values in each context to one string.

Ex.

SUPPRESS

123456

CONTEXTCHANGE

123

654

789

CONTEXTCHANGE

012

321

CONTEXTCHANGE

I want to have the output like this...

SUPPRESS

123456

CONTEXTCHANGE

123, 654, 789

CONTEXTCHANGE

012, 321

CONTEXTCHANGE

I hope you can help me out here.

Thank you!

Sten

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Hi,

try this code it will work,

String[] store = new String[1];
for(int i=0;i<var1.length;i++)
{
store[0] = store[0]+var1<i>;
}
result.addValue(store[0]);

var1 is input field containing numbers(123, 654, 789

)

Regards,

Rohit

Reward points if helpful.

Answers (3)

Answers (3)

Former Member
0 Kudos

Thank you all for your help!

Best regards,

Sten

Former Member
0 Kudos

Create a UDF o type(cache) Context, pass an string to it (your input value)

String final = null;

for(i=0;i<a.length;i++) // a = input array

{

final = final+a<i>;

}

return final;

Former Member
0 Kudos

Hi Sten,

Try with this UDF.

/**
     * Concatenates values from a queue or context to a String
     *
     * @param texts   values
     * @param separators strings to be placed between the values in <code>values</code> to build the final String
     * @return concatenation of all the values in <code>values</code>
     */
    public static String[] flattenContext(String[] texts, String[] separators) {
        StringBuffer contextBuffer;
        ArrayList result;

        contextBuffer = new StringBuffer();
        result = new ArrayList();

        for(int i = 0; i < texts.length; i++) {
            String text;

            text = texts<i>;
            if(!text.equals(ResultList.SUPPRESS)) {
                if(!text.equals(ResultList.CC)) {
                    if(contextBuffer.length() > 0) {
                        int bindersIndex;

                        bindersIndex = Math.min(i, separators.length - 1);
                        contextBuffer.append(separators[bindersIndex]);
                    }
                    contextBuffer.append(text);
                } else if(text.equals(ResultList.CC) && contextBuffer.length() > 0) {
                    result.add(contextBuffer.toString());
                    result.add(ResultList.CC);
                    contextBuffer = new StringBuffer();
                }
            }
        }

        result.add(contextBuffer.toString());

        return (String[]) result.toArray(new String[result.size()]);
    }

Don't forget apply the rewards point !

Regards,

Martin.

Former Member
0 Kudos

I got some errors...

Do I need to import something?

usr/sapxxx_.java:1373: cannot resolve symbol symbol : variable contextBuffer location: class com.sap.xi.tf._xxx_ contextBuffer = new StringBuffer();

/usr/sapxxx.java:1374: incompatible types found : java.util.ArrayList required: com.sap.aii.mappingtool.tf3.rt.ResultList result = new ArrayList();

/Sten

Former Member
0 Kudos

maybe you didnt copy the definition in line 1

StringBuffer contextBuffer;