Skip to Content
avatar image
Former Member

Multiple values in context concat to one string

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    avatar image
    Former Member
    May 27, 2008 at 06:08 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 26, 2008 at 01:03 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 26, 2008 at 01:06 PM

    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;

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 27, 2008 at 08:22 AM

    Thank you all for your help!

    Best regards,

    Sten

    Add comment
    10|10000 characters needed characters exceeded