Remove plus sign from beginning and replace whitespaces of number

4.4k views Asked by At

I need edit the string variable in Tasker "Variable Search Replace" but dont recognize the special characters.

I need edit below string

+70 888 777 1 1 3
to;
70888777113

How can i achieve this?

5

There are 5 answers

3
karthik manchala On BEST ANSWER

You can use the following to match (Easy approach):

\D               //(any non digit)

And replace with '' (empty string)

See DEMO

Code:

str = str.replaceAll("\\D", "");

Edit: If your string is a part of another string use the following:

(?<=\d)\s+(?=\d)|\+(?=\d)

See DEMO

Explanation:

  • (?<=\d)\\s+(?=\d) All white spaces surrounded by digits
  • \+(?=\d) plus sign in the beginning of digits
0
Blackbelt On

if String tmp = "+70 888 777 1 1 3";

then

   tmp = tmp.replaceAll("\\s+", "");

will remove all the white space from tmp, and

if (tmp.startWith("+")) {
    tmp = tmp.substring(1, tmp.length());
}

will remove the +

1
Saloni Uppal On

You may replace all white spaces from string by using following :

 $string = preg_replace('/\s+/', '', $string);
0
Attaullah On

try this code it worked fine in java

String str = "+1234567";
if(str.substring(0, 0).equals("+")){
   // assign value string value expect first character 
   str = str.substring(1);
}
0
user8303532 On

Use this below code String tmp = "+70 888 777 1 1 3"; tmp replaceAll("[^0-9]+","");