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?

0

6 Answers 6

10

You can do like this

String str = "09XX-XXX-XXXX"; str = "+63"+str.substring(1); 
Sign up to request clarification or add additional context in comments.

Comments

1

Replacing the first "0" is as simple as:

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

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

Comments

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

try this it will replace first character from the string

4 Comments

how can I replace the 0 into '+63'?
give whole text and output to be get
my number is 07875780025 then it will convert all 0 to +63. i need only first 0 is change.
Use as mention below str="+63"+str.substring(1,str.length());
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; } 

Comments

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); 

3 Comments

This is incorrect , it will replace '0' with '+63' in the whole string . its not what he has asked.
i may not understand question.
my number is 07875780025 then it will convert all 0 to +63. i need only first 0 is change.
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"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.