Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

saprating strings with commas one by one

 
Arun M'Purushothaman
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to get a single string from many strings which should be connected with commas. how i can do it with java?
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using a StringBuffer and then loop through the Strings that you wish to concetnate passing it to the append() method on the StringBuffer.
 
Rohit Ahuja
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy,
Give him the code dear.
Okie here u go,
Assuming i have a 100 strings in a String array arr.
StringBuffer stringbuffer = new StringBuffer(200);
for(int i = 0;i < arr.length; i++ )
{
if(!String.valueOf( arr[i] ).equals("null"))
{
if(i != 0)stringbuffer.append(",");

stringbuffer.append( arr[i] );
}
}
System.out.println( stringbuffer );
Give it a shot dude.Tell me if it doesnt work.Try some R&D okie?? I havent tried compiling and runing the program.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic