Search code examples
apex-code

Unable to remove last comma from string


I am trying to remove the last comma in a string using the substring method:

system.debug('******************* quoteQuery ' + quoteQuery);
quoteQuery = quoteQuery.subString(0,quoteQuery.Length()-1);
system.debug('******************* quoteQuery ' + quoteQuery);

This is the string debug output:

DEBUG|******************* quoteQuery SELECT phone,  lineitemcount,  additionalstreet,  expirationdate,  shippingname,  contactid,  createdbyid,  shippinghandling,  additionalstate,  billingcity,  description,  isdeleted,  quotetostate,  quotenumber,  systemmodstamp,  fax,  totalprice,  status,  quotetocountry,  shippingcity,  quotetopostalcode,  billingpostalcode,  shippingstate,  createddate,  subtotal,  discount,  tax,  email,  shippingpostalcode,  lastmodifiedbyid,  additionalcountry,  billingstate,  billingcountry,  lastmodifieddate,  id,  name,  pricebook2id,  shippingstreet,  opportunityid,  quotetocity,  additionalpostalcode,  billingname,  additionalcity,  shippingcountry,  additionalname,  grandtotal,  billingstreet,  issyncing,  quotetoname,  quotetostreet, 

The last comma is not getting removed?

Any help is appreciated. Thanks.


Solution

  • This will return the complete string

    quoteQuery.subString(0,quoteQuery.Length()-1);
    

    So instead of Lenght()-1 try Length()-2

    quoteQuery = quoteQuery.subString(0,quoteQuery.Length()-2);