+ Start a Discussion
mattpick1mattpick1 

Spaces in phone number, String charAt()

Hi,

 

I am writing an Apex trigger which contains code to match a phone number to a contact phone number. It should match numbers that contain different spaces between the digits (when people store them they may store them in different formats).

 

First I thought of pulling out all the contacts in a query, then cyling through them using the trim() function on a string to match them. This seems wasteful though and there might be more contacts than the maximum number of records you are allowed to return.

 

So I then thought of creating a new string from the phone number I am matching with a '%' before and after each digit. 

Eg. '07734654342' would become '%0%7%7%3%4%6%5%4%3%4%2%' then embedding that string in a query like:

//The phone string with the % chars is wildPhone Contact[] contact = SELECT Name FROM Contact WHERE Phone LIKE :wildPhone];

 I'm not sure if this will work, but more importantly how do I go about creating that string? I thought there would be a charAt() method on the string like in Java...I can't find one though.

 

Thank you for your help.

 

Matt

 

Best Answer chosen by Admin (Salesforce Developers) 
rocwilcoxrocwilcox

String tamePhone;

tamePhone='111-222-3333';  //example

 

for (Integer i = 0; i < tamePhone.length; i++)

{

  wildPhone += '%' + tamePhone.subString(i,1);

}

wildPhone += '%';

 

 

All Answers

rocwilcoxrocwilcox

String tamePhone;

tamePhone='111-222-3333';  //example

 

for (Integer i = 0; i < tamePhone.length; i++)

{

  wildPhone += '%' + tamePhone.subString(i,1);

}

wildPhone += '%';

 

 

This was selected as the best answer
mattpick1mattpick1

Thanks! That looks like it will work, now to check that my wildcard plan will....

 

NB for the substring I think it should be tamePhone.substring(i, i+1), having an endIndex of 1 doesn't make sense to me unless the function was substring(startIndex, lengthOfSubstring) which I don't think it is.

Message Edited by mattpick1 on 12-23-2009 01:09 AM
turbo2ohturbo2oh

Were you ever able to solve this issue?