Skip to main content
Notice removed Needs detailed answers by Jamal
deleted 25 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I want to join strings together, but when doing this it often happens that there is a comma too muchmany, and therefore I need to remove that comma. In the belowthis code, I use the substring to delete the two last characters.

Is there aHow can this become more elegant way to solve this problem?

List<String> paramList = new ArrayList<String>( ); paramList.add( "param1" ); paramList.add( "param2" ); StringBuilder result = new StringBuilder(); for ( String p : paramList ) { result.append( p ).append( ", " ); } String withoutLastComma = result.substring( 0, result.length( ) - ", ".length( ) ); System.err.println( withoutLastComma ); 

I want to join strings together, but when doing this it often happens that there is a comma too much, and therefore I need to remove that comma. In the below code I use the substring to delete the two last characters.

Is there a more elegant way to solve this problem?

List<String> paramList = new ArrayList<String>( ); paramList.add( "param1" ); paramList.add( "param2" ); StringBuilder result = new StringBuilder(); for ( String p : paramList ) { result.append( p ).append( ", " ); } String withoutLastComma = result.substring( 0, result.length( ) - ", ".length( ) ); System.err.println( withoutLastComma ); 

I want to join strings together, but when doing this it often happens that there is a comma too many, and therefore I need to remove that comma. In this code, I use the substring to delete the two last characters.

How can this become more elegant?

List<String> paramList = new ArrayList<String>( ); paramList.add( "param1" ); paramList.add( "param2" ); StringBuilder result = new StringBuilder(); for ( String p : paramList ) { result.append( p ).append( ", " ); } String withoutLastComma = result.substring( 0, result.length( ) - ", ".length( ) ); System.err.println( withoutLastComma ); 
provided more pure-english description, had to change the title because of "Problem" not being allowed
Source Link
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312

Remove-Last-Comma-Problem Last Comma

I want to join strings together, but when doing this it often happens that there is a comma too much, and therefore I need to remove that comma. In the below code I use the substring to delete the two last characters.

Is there a more elegant way to solve this problem?

List<String> paramList = new ArrayList<String>( ); paramList.add( "param1" ); paramList.add( "param2" ); StringBuilder result = new StringBuilder(); for ( String p : paramList ) { result.append( p ).append( ", " ); } String withoutLastComma = result.substring( 0, result.length( ) - ", ".length( ) ); System.err.println( withoutLastComma ); 

Remove-Last-Comma-Problem

is there a more elegant way to solve this problem?

List<String> paramList = new ArrayList<String>( ); paramList.add( "param1" ); paramList.add( "param2" ); StringBuilder result = new StringBuilder(); for ( String p : paramList ) { result.append( p ).append( ", " ); } String withoutLastComma = result.substring( 0, result.length( ) - ", ".length( ) ); System.err.println( withoutLastComma ); 

Remove Last Comma

I want to join strings together, but when doing this it often happens that there is a comma too much, and therefore I need to remove that comma. In the below code I use the substring to delete the two last characters.

Is there a more elegant way to solve this problem?

List<String> paramList = new ArrayList<String>( ); paramList.add( "param1" ); paramList.add( "param2" ); StringBuilder result = new StringBuilder(); for ( String p : paramList ) { result.append( p ).append( ", " ); } String withoutLastComma = result.substring( 0, result.length( ) - ", ".length( ) ); System.err.println( withoutLastComma ); 
Notice added Needs detailed answers by 200_success
Notice removed Needs detailed answers by Jamal
Question Protected by Jamal
Notice added Needs detailed answers by Jamal

is there a more elegant way to solve this problem?

List<String> paramList = new ArrayList<String>( ); paramList.add( "param1" ); paramList.add( "param2" ); StringBuilder result = new StringBuilder(); for ( String p : paramList ) { result.append( p ).append( ", " ); } String withoutLastComma = result.substring( 0, result.length( ) - ", ".length( ) ); System.err.println( withoutLastComma ); 

Thanks a lot.

is there a more elegant way to solve this problem?

List<String> paramList = new ArrayList<String>( ); paramList.add( "param1" ); paramList.add( "param2" ); StringBuilder result = new StringBuilder(); for ( String p : paramList ) { result.append( p ).append( ", " ); } String withoutLastComma = result.substring( 0, result.length( ) - ", ".length( ) ); System.err.println( withoutLastComma ); 

Thanks a lot.

is there a more elegant way to solve this problem?

List<String> paramList = new ArrayList<String>( ); paramList.add( "param1" ); paramList.add( "param2" ); StringBuilder result = new StringBuilder(); for ( String p : paramList ) { result.append( p ).append( ", " ); } String withoutLastComma = result.substring( 0, result.length( ) - ", ".length( ) ); System.err.println( withoutLastComma ); 
Tweeted twitter.com/#!/StackCodeReview/status/60447509453086720
Source Link
Jonas
  • 1.8k
  • 2
  • 12
  • 5
Loading