on 11-25-2008 7:46 PM
Hello
Can anyone please help me with this issue.
We want to convert an array of elements into a comma seperated string. Can you please guide us on this issue.
The structure is as follows
VendorID
Item0
Item1
Item2
Item3
to a string with Item0,Item1,Item2,Item3
Thanks.
Kiran
udf as below...
function convertArrayToString( String[] elements )
{
StringBuffer s = new StringBuffer();
for (int i = 0; i < elements.length; i++ )
{
s.append ( elements[ i ] );
if ( i != ( elements.length - 1 ) )
{
s.append ( "," );
}
}
return s.toString();
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Kiran
Try this simple UDF type context
public void getitem(String[] items,ResultList result,Container container){
for(int i=0;i<items.length;i++){
result.addValue(items<i>+",");
}
Replace items with parent node of items and map it to parent node. or keep items your choice
Irrespective of number of nodes under parent node it will add comma to all and return to single field.
Thanks
Gaurav
>
public void getitem(String[] items,ResultList result,Container container){
> for(int i=0;i<items.length;i++){
> result.addValue(items<i>+",");
> }
>
Gaurav,
I doubt this gonna work. With the above code, this will be output i guess.
Input:
Vendor
item1
item2
item3
Output:
item1,
item2,
item3,
You need to use addValue only once.. probably following code will work
public void getitem(String[] items,ResultList result,Container container)
{
StringBuffer b = new StringBuffer();
for(int i=0;i<items.length;i++){
b.append( items<i> ).append(",");
}
result.addValue( b.toString( ) );
Hey Anand
Its working for me and i checked in my system before providing to Kiran
Well to execute result.add in a loop is doing the same operation as append
Try it once. let me also know the case where it failed so that i can change the code as per your directions.
We can improve this and Kiran get the best out.:)
Thanks
Gaurav
Thanks Anand and Gaurav,
I am using the following code which is the combined code provided by both of you.
public void calculate(String[] items, ResultList result, Container container)
{
StringBuffer b = new StringBuffer();
for(int i=0;i<items.length;i++)
{
b.append( items<i> );
if( i != ( items.length - 1 ) )
b.append(",");
}
result.addValue( b.toString( ) );
}
I appreciate all your help with the issue.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.