How to find substring of string after particular character using Apex in Salesforce?

substringAfter() is used to find substring of string after particular character using Apex in Salesforce

Sample Code:

String str = 'abc-xyz';
system.debug('Expected is ' + str.substringAfter('-'));

Output:


6 comments:

  1. How about finding multiple substrings in a string ?

    Ex: String str = 'abc-xyz-123';

    ReplyDelete
    Replies
    1. Use split method.
      Example - https://www.infallibletechie.com/2020/03/string-split-method-in-apex-in.html

      Delete
  2. how we can use this function in report builder

    ReplyDelete
    Replies
    1. Report builder doesn't support this.
      The example is for using Apex.

      Delete
  3. I have list of Strings like
    blList = (QA-ASW,QA-WSA,AQ-WWQ,AQ-QAW)
    I want split the strings in the list based on '-' and get pre text...For ex: in QA-ASW i want to get QA,
    So as per the blList values I want to get a list like (QA, QA, AQ, AQ).

    Kindly your inputs would be of great help, It is critical task i am working

    ReplyDelete
    Replies
    1. Use the following
      String str1 = 'QA-ASW,QA-WSA,AQ-WWQ,AQ-QAW';
      for ( String str : str1.split( ',' ) ) {

      String tempStr = str.split( '-' ).get( 0 );
      system.debug( tempStr );

      }

      Delete