Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.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_String_Text_Merge_Concatenation - Fatal编程技术网

在java代码中用一个字符串替换多个字符串

在java代码中用一个字符串替换多个字符串,java,string,text,merge,concatenation,Java,String,Text,Merge,Concatenation,我需要替换: myVariable = "sample string is long " + "so I put rest of it in 2nd line." + " And sometimes in 3rd and so on"; 与: 附加问题:如何合并上面提到的实体,它们在连接链中有其他变量 myVar = "The number of the beast is " + numberOfTheBeast + " !!! So I said"; 我想做的是将其更改为包含参数的

我需要替换:

myVariable = "sample string is long " +
  "so I put rest of it in 2nd line." +
  " And sometimes in 3rd and so on";
与:

附加问题:如何合并上面提到的实体,它们在连接链中有其他变量

myVar = "The number of the beast is " + numberOfTheBeast + " !!! So I said";

我想做的是将其更改为包含参数的单个字符串。

我不是正则表达式专家,但这三个正则表达式的替换应该可以做到这一点。大多数IDE都有一个正则表达式替换函数

Replace:     With:
\s*\+\s*\n    +   (space, plus, space)
"\s*\+\s*"   nothing
\s*\+\s*      +   (space, plus, space)

如果您在Windows上,可能必须使用\s*+\s*\r\n作为第一个正则表达式。

不理解您的问题。请不要在一个问题中发布多个问题。您的第一个问题是一个特定于IDE的问题—您正在使用哪个IDE?也。。。你为什么要这样做?你是说在代码中还是在运行时?我真的不明白这个问题本身。让我试着解释一下为什么我需要这样做。我有这段代码要维护,我需要去掉那些字符串连接(也要去掉那些字符串)。
Replace:     With:
\s*\+\s*\n    +   (space, plus, space)
"\s*\+\s*"   nothing
\s*\+\s*      +   (space, plus, space)