1

I'm trying to remove a substring with \n + random dateTime value + created date time.. My code works, but I want to make it prettier. How do I remove repeating parts by different conditions? If it's possible.

   if(ground.Source__c != null && ground.Source__c.containsIgnoreCase('Random source')){
        if(actualVacancy.Comment__c != null){
            String commentPart = '\n' + actualVacancy.CreatedDate + ' created date time.';
            if(actualVacancy.Comment__c.contains(commentPart)){

                Integer commentStartIndex;
                //I'm trying  to find here the first index of commentPart
                //if this is the beginning of the comment, then excluding '\n':
                if(actualVacancy.Comment__c.indexOf(commentPart) - 19 == 0){
                    commentStartIndex = actualVacancy.Comment__c.indexOf(commentPart) - 19;
                //if it's not the beginning of the comment, then with '\n':
                } else commentStartIndex = actualVacancy.Comment__c.indexOf(commentPart) - 20;

                Integer commentFinishIndex = actualVacancy.Comment__c.indexOf(commentPart) + commentPart.length();
                String toRemovePart = actualVacancy.Comment__c.substring(commentStartIndex, commentFinishIndex);
                actualVacancy.Comment__c = actualVacancy.Comment__c.remove(toRemovePart);
            }
            actualVacancy.Comment__c += cleaningComment;
        } else actualVacancy.Comment__c = cleaningComment; 
    } else{
        //And here is code duplication:
        if(actualVacancy.Comment__c != null && actualVacancy.Comment__c.contains(commentPart)){

            Integer commentStartIndex;     
            if(actualVacancy.Comment__c.indexOf(commentPart) - 19 == 0){             
                commentStartIndex = actualVacancy.Comment__c.indexOf(commentPart) - 19;                                
            } else commentStartIndex = actualVacancy.Comment__c.indexOf(commentPart) - 20;

            Integer commentFinishIndex = actualVacancy.Comment__c.indexOf(commentPart) + commentPart.length();
            String toRemovePart = actualVacancy.Comment__c.substring(commentStartIndex, commentFinishIndex);  
            actualVacancy.Comment__c = actualVacancy.Comment__c.remove(toRemovePart); 
        }
    }

1 Answer 1

4

We usually move the reusable piece of code to a common utility method. You can create a private method that will return the sanitized comment back.

private static String getSanitizedComment(String input , String commentPart){

    Integer commentStartIndex;
    //I'm trying  to find here the first index of commentPart
    //if this is the beginning of the comment, then excluding '\n':
    if(input.indexOf(commentPart) - 19 == 0){
        commentStartIndex = input.indexOf(commentPart) - 19;
    //if it's not the beginning of the comment, then with '\n':
    } else {
        commentStartIndex = input.indexOf(commentPart) - 20;
    }
    Integer commentFinishIndex = input.indexOf(commentPart) + commentPart.length();
    String toRemovePart = input.substring(commentStartIndex, commentFinishIndex);
    String outputString = input.remove(toRemovePart);
    return outputString;

}

Use:

if(ground.Source__c != null && ground.Source__c.containsIgnoreCase('Random source')){
            if(actualVacancy.Comment__c != null){
                String commentPart = '\n' + actualVacancy.CreatedDate + ' created date time.';
                if(actualVacancy.Comment__c.contains(commentPart)){
                    actualVacancy.Comment__c = getSanitizedComment(actualVacancy.Comment__c ,commentPart );
                }
                actualVacancy.Comment__c += cleaningComment;
            } else {
                actualVacancy.Comment__c = cleaningComment;
            }
        } else{
            //And here is code duplication:
            if(actualVacancy.Comment__c != null && actualVacancy.Comment__c.contains(commentPart)){
                actualVacancy.Comment__c = getSanitizedComment(actualVacancy.Comment__c ,commentPart ); 
            }
        }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .