Remove the plus sign from the beginning and replace spaces with numbers

I need to change a string variable in Tasker "Replace Search Variables", but not recognize special characters.

I need to change the line below

+70 888 777 1 1 3
to;
70888777113

      

How can I achieve this?

+3


source to share


5 answers


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

\D               //(any non digit)

      

And replace with ''

(empty string)

See DEMO

Code:

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

      



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

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

      

See DEMO

Explanation:

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

    All spaces surrounded by numbers
  • \+(?=\d)

    plus sign at the beginning of digits
+2


source


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

then

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

      



will remove all spaces from tmp

and

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

      

will remove +

+1


source


You can replace all spaces with a string using the following command:

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

      

+1


source


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


source


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

0


source







All Articles