Homework, right?
Convert the String to a char array with toCharArray() and pull out what you want - i.e.:
String wholePhoneNumber = "2125551212";
char[] numberAsAnArray = wholePhoneNumber.toCharArray();
char[] areaCodeArray = new char[3];
areaCodeArray[0] = numberAsAnArray[0];
areaCodeArray[1] = numberAsAnArray[1];
...
There are more efficient ways but this is likely to get the problem solved.
Thanks for the reply but can't i do using text formatter. the problem is when i am using substring i am getting array out of bound exception because substring is not holding the maximum number which i have given. the phone number is dynamic it can have 2 spaces or 4 spaces but format output should be xxx-xxx-xxxx
how about message formatting? how it works