Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中用替换_Java_Regex_String_Replace - Fatal编程技术网

如何在java中用替换

如何在java中用替换,java,regex,string,replace,Java,Regex,String,Replace,我有这样一个字符串=[“41020834846-1”。我想用replaceAll删除字符串开头的([“) 那我该怎么办 我尝试了replaceAll(“[\”“,”),但没有成功。只需使用replace(“[\”“,”) replaceAll(…)用于regexp-match。如果要使用replaceAll执行此操作,则需要转义正则表达式中使用的符号[: replaceAll("\\[\"", "") 如果字符串始终在开头,则只需执行以下操作: .substring(2); 只需使用repl

我有这样一个字符串=
[“41020834846-1”
。我想用replaceAll删除字符串开头的(
[“

那我该怎么办

我尝试了
replaceAll(“[\”“,”)
,但没有成功。

只需使用
replace(“[\”“,”)

replaceAll(…)
用于regexp-match。如果要使用replaceAll执行此操作,则需要转义正则表达式中使用的符号
[

replaceAll("\\[\"", "")
如果字符串始终在开头,则只需执行以下操作:

.substring(2);
只需使用
replace(“[\”“,”)

replaceAll(…)
用于regexp-match。如果要使用replaceAll执行此操作,则需要转义正则表达式中使用的符号
[

replaceAll("\\[\"", "")
如果字符串始终在开头,则只需执行以下操作:

.substring(2);
试试下面的代码

String test = "[\"41020834846 - Yan Oda 1";
Log.i("======= Replace String"," :: "+test.replace("[\"", ""));
输出-
41020834846-1

尝试下面的代码

String test = "[\"41020834846 - Yan Oda 1";
Log.i("======= Replace String"," :: "+test.replace("[\"", ""));
输出-
41020834846-1

您可以使用以下方法:

您可以使用以下方法:


您需要使用
replaceAll(“\[\”“,”)
,因为
[
是regex语法的一部分,因此您需要为您的案例输入无效的regex

如果您经常处理文本,还可以使用ApacheCommonsStringuitls或Guava。 例如,使用StringUtils,例如:

 StringUtils.remove("queued", "ue") = "qd"

您需要使用
replaceAll(“\[\”“,”)
,因为
[
是regex语法的一部分,因此您需要为您的案例输入无效的regex

如果您经常处理文本,还可以使用ApacheCommonsStringuitls或Guava。 例如,使用StringUtils,例如:

 StringUtils.remove("queued", "ue") = "qd"
试试看:

 String example = "["41020834846 - Yan Oda 1";
    String pattern = "^(\[\")";
    example = example.replaceAll(pattern,""));
试试看:

 String example = "["41020834846 - Yan Oda 1";
    String pattern = "^(\[\")";
    example = example.replaceAll(pattern,""));

你不应该使用

replaceAll();
使用

replace();
方法。 另一种方法是使用字符串类方法


祝你好运!

你不应该使用

replaceAll();
使用

replace();
方法。 另一种方法是使用字符串类方法


祝你好运!

这是一种模式-
[“
只是在开始的时候吗?是的。但是我现在解决了这个问题。这是一种模式-
[”
仅在开始时?是的。但我现在解决了问题。愚蠢的我,我尝试过这个,但忘记了支票。但是非常感谢提醒和回答。愚蠢的我,我尝试过这个,但忘记了支票。但是非常感谢提醒和回答。