[SalesForce] how to split string based on particular digit in apex

Hi i will get 18 digit recordId in batch apex..But i have 15 digit Id.When id tried to compare both ids getting null value.How to get 15 digit id from 18 digit id.

String id = 'a0P4E0000030HzNXAU';
i need only 'a0P4E0000030HzN'

Best Answer

The following should work:

String id ='a0P4E0000030HzNXAU';

String Str= id.substring(0, 15);

Signature

public String substring(Integer startIndex, Integer endIndex)

Returns a new String that begins with the character at the specified zero-based startIndex and extends to the character at endIndex - 1.

Parameters

  • startIndex Type: Integer

  • endIndex Type: Integer

  • Return Value Type: String

Related Topic