SUBSTR alternate in apex

767    Asked by AmyAvery in Big Data Hadoop , Asked on Jul 12, 2021

JS:

var str = "SLA-UPsdfdsfdsfG-4HR-4ON"; var res = str.substr(-7, 3);

Output: 4HR

What is the alternative for Substr in apex which is starting from end due to minus starting index.

Answered by Ben PHILLIPS

Another alternative for Substr in apex is as follows:

You can use a combination of Right and Substring method to achieve the desired output in Apex. Below code for your reference.

        String str = 'SLA-UPsdfdsfdsfG-4HR-4ON'; String res = str.right(7).substring(0, 3); System.debug('res is::'+res);

The result would be 4HR for the above code. What is salesforce string class ?

The salesforce String class represents character strings. All string literals in Java programs, such as "abc" , are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. ... Because String objects are immutable they can be shared.



Your Answer

Interviews

Parent Categories