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?
Asked
Active
Viewed 7,685 times
6 Answers
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);

Oladapo Omonayajo
- 992
- 9
- 14
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
-
how can I replace the 0 into '+63'? – androidBoomer Sep 27 '13 at 09:09
-
give whole text and output to be get – Ali Ahmad Sep 27 '13 at 09:12
-
my number is 07875780025 then it will convert all 0 to +63. i need only first 0 is change. – Ramesh Bhati Feb 26 '18 at 06:36
-
Use as mention below str="+63"+str.substring(1,str.length()); – Ali Ahmad Feb 26 '18 at 09:05
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
-
2This is incorrect , it will replace '0' with '+63' in the whole string . its not what he has asked. – Jitender Dev Sep 27 '13 at 09:20
-
-
my number is 07875780025 then it will convert all 0 to +63. i need only first 0 is change. – Ramesh Bhati Feb 26 '18 at 06:33
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");