how to remove special character from string except + in android? - android

I am fetching number from contact book and sending it to server. i get number like this (+91)942 80-60 135 but i want result like this +9428060135.+ must be first character of string number.

Given your example you want to replace the prefix with a single + character. You also want to remove other non-numeric characters from the number string. Here's how you can do that:
String number = "(+91)942 80-60 135";
number = "+" + number.replaceAll("\\(\\+\\d+\\)|[^\\d]", "");
The regex matches any prefix (left paren followed by a + followed by one or more digits, followed by a right paren) or any non digit character, and removes them. This is concatenated to a leading + as required. This code will also handle + characters within the number string, e.g. +9428060135+++ and +(+91)9428060135+++.
If you simply wanted to remove any character that is not a digit nor a +, the code would be:
String number = "(+91)942 80-60 135";
number = number.replaceAll("[^\\d+]", "");
but be aware that this will retain the digits in the prefix, which is not the same as your example.

You can use String.replace(oldChar, newChar). Use the code below
String phone = "(+91)942 80-60 135"; // fetched string
String trimmedPhone = phone.replace("(","").replace(")","").replace("-","").trim();
I hope it will work for you.

check this. Pass your string to this function or use as per code goes
String inputString = "(+91)942 80-60 135";
public void removeSpecialCharacter(String inputString) {
String replaced = inputString.replaceAll("[(\\-)]", "");
String finalString = replaced.replaceAll(" ", "");
Log.e("String Output", " " + replaced + " " + second);
}

Related

String with \n in single string android

I have multiple value in single string, but I want to send in every string value on new line. I have written the following code, but it's not working.
String text ="Address" + strpropertyAddress +"\n"+ "Price" + strPrice;
This is my service method where i pass the string.
sendPropertyApi(text, sendto);
I have checked every where, and it is exactly same. Basically the string data should be sent in on a new line, but it's sent in on a single line.
Assuming(Because of Android tag) you are setting this text in textView or editText, for that I think you have to escape \. So use \\n instead. Try following:
String text ="Address" + strpropertyAddress +"\\n"+ "Price" + strPrice;
Or it is always better to use default line separator like following:
String text ="Address" + strpropertyAddress +System.getProperty("line.separator")+ "Price" + strPrice;

How to fetch number from text in android

I want use register in my application and i should send password and verifyCode with SMS to users phones.
But i should read verifyCode from message and set automatically number into verifyCode EditText.
My message format :
Hi, welcome to our service.
your password 12345
your verifyCode 54321
How can i do it? Please help me <3
Assuming that the number of digits are fixed in password and verify codes (Generally they are same as default values), We can extract digits from the string and then find substring which has verify code. This assumption is for simplicity.
String numberOnly= str.replaceAll("[^0-9]", "");
String verifyCode = numberOnly.substring(6);
Here String verifyCode = numberOnly.substring(6); is getting last 5 digits of the string which is your verification code. You can also write numberOnly.substring(6,10); to avoid confusions.
But this is prone to errors like StringIndexOutOfBoundsException, So whenever you want to get substring which is starting from index i till the end of the string, always write numberOnly.substring(i).
There are a lot ways to do this. You can use some complicated regex or use a simple spilt method.
Try this,
String str = "Hi, welcome to our service.\n"
+ "\n"
+ "your password \n"
+ "12345\n"
+ "\n"
+ "your verifyCode \n"
+ "54321";
// Solution #1
String[] parts = str.split("\n");
System.out.println(parts[3]);
System.out.println(parts[6]);
// Solution #2
String PAT = "(password|verifyCode)\\s+(\\d+)";
Pattern pats = Pattern.compile(PAT);
Matcher m = pats.matcher(str);
while (m.find()) {
String grp = m.group(2);
System.out.println(grp);
}

String to display number currency in Android

I have a string
String retail = c.getString(c.getColumnIndex("retail"));
The date is being passed as "99999", I need it to print out as "999.99", how can I do this?
If you always have to add the "." 2 character before the end, this should work:
retail = retail.substring(0, retail.length()-2) + "." + retail.substring(retail.length()-2,retail.length());
This will add a dot two character before the end of the String, as you need.

Checking if string contains sentence with variable words

It should be something like "Hello, my name is ??????????." in Open Office Base.
I am receiving a string from a website which is always build up like this:
"Hello, my name is " +name+ "."
The variable name can be ANY name with a length of up to 10 characters and it changes everyday.
I am trying to check if the string, that is provided by the website, contains this sentence (Example: "Hello, my name is John.").
BUT I don't know the name that the website provides, so I have to ask:
if(string1.contains("Hello, my name is " + ANY 10 CHARACTERS + "."))
{
return true;
}
First need to parse out the name from the String by removing all characters besides the name.
String name = stringFromWebsite.replace("Hello, my name is ", ""); //remove "Hello, my name is "
name = name.substring(0, name.length() - 1) //remove "." at the end
name = name.trim(); //trim any whitespaces
Next concatenate the base String + your variable.
String newString = "Hello, my name is " + name;
Now run the contains function against the new String
if (string1.contains(newString)){
return true;
}

How to replace character at particular index in android?

I am working on android. I have a string containing huge data. In that string I want to replace a particular character to another character. I got the index of the character which I want to replace. But I am unable to replace that character.
How can I do that?
String str = "data1data2mdata2test1test2test3dd"
int ind = str.indexOf("m");
System.out.println("the index of m" + ind);
Now in the above string I want to replace the character "m"(after data2) to "#".
Now how can I replace the m to #. Please help me in this reagard.
You can use substring:
String newStr = str.substring(0, ind) + '#' + str.substring(ind + 1);
Try this:
str = str.replaceFirst("m", "#");
It will replace the first m to #
String str1 = "data1data2mdata2test1test2test3dd"
String str = str1.replace("m", "#");
System.out.println(str);
So you are getting 10 as system out,
so this way you can replace it like,
Str.replace('m', '#')--->when you want all occurrences of it to replace it,
Or if you want only first occurrence to be replaced by # then you can do following trick,
StringBuffer buff=new StringBuffer();
buff.append(Str.substring(0,ind)).append("#").append(Str.substring(ind+1));
i hope it would help

Categories

Resources