0

I am doing an app wherein the user is required to enter his mobile number. In order for my app to be user friendly, I am providing a format for them (e.g. 09XX-XXX-XXXX). After the registration process, the app will automatically text a certain number which compose the user's FirstName, Last Name and Mobile Number. In order for the server to contact the user, I must replace the '0' (which is the first character) with '+63'. Can somebody help me on how to replace it?

androidBoomer
  • 3,357
  • 6
  • 32
  • 42

6 Answers6

10

You can do like this

String str = "09XX-XXX-XXXX";
str = "+63"+str.substring(1);
NARESH REDDY
  • 682
  • 4
  • 11
1

Replacing the first "0" is as simple as:

String phone = "09XX-XXX-XXXX";

phone = "+63"+phone.substring(1);

0
String str="09XX-XXX-XXXX";
str=str.substring(1,str.length());

try this it will replace first character from the string

Ali Ahmad
  • 1,351
  • 9
  • 11
0

You have to change the first character only if it is written by user use below code it will also adds validation if zero is inserted by user or not.

String str = "09XX-XXX-XXXX";


if(str.substring(0,1).equals("0"))
        {
            str = "+63"+str.substring(1);

        }
else
{
str = "+63"+str;
}
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
0

just try this..

    String someString = "0";
    String str = someString.replace("0", "+63");
    Log.v("hari", "str:"+str);

    or

     String someString = "0 9854172 common";
    String str = someString.replace("0", "+63");
    Log.v("hari", "str:"+str);
harikrishnan
  • 1,985
  • 4
  • 32
  • 63
0

The question is duplicate of How to replace a plus character using Java's String.replaceAll method. Try the followings.

String str = "09XX-XXX-XXXX";
str.replaceFirst("0", "\\+63");
Community
  • 1
  • 1
Pradip
  • 3,189
  • 3
  • 22
  • 27