function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ramkramk 

Reverse a phone number logic in apex

Hi

Is it possible to compress phone # is the reverse of the phone number  (example phone #  2177627123 search phone # = 3217267712) in apex?????

thanks

 

 

goabhigogoabhigo

There is no pre-built method to do this, but you can make use of string methods like length, substring (more info here - http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_string.htm)

 

May be copy each number starting from end [ Phone.substring(phone.length()-1, phone.length()) ] from last character and repeat this in a loop by decreasing the startIndex and endIndex of substring method.

 

Helps?

SLockardSLockard

To illustrate what abhi was saying, try something like this:

String forward = '2177627123';
String reverse = '';

for (Integer i = forward.length()-1; i >= 0; i--)
{
	reverse += forward.substring(i, i+1);
}

system.debug(reverse);

 

cropzimcropzim

Winter 13 adds a new String method 'reverse' as in String sReversed = s.reverse();  See https://na2.salesforce.com/help/doc/en/salesforce_winter13_release_notes.pdf

Malika Pathak 9Malika Pathak 9

Hi ramk,

public class ReverseNumber {
public static void getreverseNumber(integer Number)
{
integer Number2=Number;
integer rev=0;
while(Number2>0)
{
rev=(rev*10)+math.mod(Number2,10);
Number2=Number2/10;
}
System.debug('reverse1 --> '+num);
System.debug('reverse --> '+rev);
}
}
if you find this helpful then mark it as the best answer.