Activity Forums Salesforce® Discussions Splitting the string removes the delimiter

  • Splitting the string removes the delimiter

    Posted by Ajit on April 12, 2016 at 11:32 am

    When I am Splitting any string using the split() method then it is working good but the problem is that after splitting it removes the char that is used foe splitting

     

    like

    String str = 'abc-xyz-pqrs-zykp';

    list<String>test = str.split('-');

    Output : {abc,xyz,pqrs,zykp}

    Actual outPut that I want : {abc,-xyz,-pqrs,-zykp};

    I want this to be done without iterating the above list in a for loop.

    Parul replied 5 years, 7 months ago 3 Members · 2 Replies
  • 2 Replies
  • Nitish

    Member
    April 12, 2016 at 11:39 am

    Hi Ajit,

    Please try this, Add this '?=' character before the value on which you want to split,

    String str = ‘abc-xyz-pqrs-zykp’;

    listtest = str.split(‘?=-‘);

    Output : {abc,-xyz,-pqrs,-zykp}

    OR if you want the dash '-' at the end of your list values than add this "(?!-)" before the value.
    String str = ‘abc-xyz-pqrs-zykp’;

    listtest = str.split(‘?!-‘);

    Output : {abc-,xyz-,pqrs-,zykp}

  • Parul

    Member
    September 7, 2018 at 4:37 am

    Hi Ajit,

    Please try this, Add this ‘?=’ character before the value on which you want to split,

    String text = "Good morning. Have a good class. " +
    "Have a good visit. Have fun!";

    String[] words = text.split("\\W+");

     

    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos