从开头删除加号并替换数字的空格

Remove plus sign from beginning and replace whitespaces of number

我需要在 Tasker 中编辑字符串变量 "Variable Search Replace" 但无法识别特殊字符。

我需要在字符串下方进行编辑

+70 888 777 1 1 3
to;
70888777113

我怎样才能做到这一点?

你可以使用下面的方法来匹配(简单的方法):

\D               //(any non digit)

并替换为 ''(空字符串)

DEMO

代码:

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

编辑: 如果您的字符串是另一个字符串的一部分,请使用以下内容:

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

DEMO

解释:

  • (?<=\d)\s+(?=\d) 全部由数字包围的空格
  • \+(?=\d) 数字开头加号

如果String tmp = "+70 888 777 1 1 3";

然后

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

将从 tmp

中删除所有白色 space
if (tmp.startWith("+")) {
    tmp = tmp.substring(1, tmp.length());
}

将删除 +

您可以使用以下内容替换字符串中的所有空格:

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

试试这个代码,它在 java

中运行良好
String str = "+1234567";
if(str.substring(0, 0).equals("+")){
   // assign value string value expect first character 
   str = str.substring(1);
}

使用下面的代码 字符串 tmp = "+70 888 777 1 1 3"; tmp replaceAll("[^0-9]+","");