简体   繁体   English

在两个字符串之间替换字符串

[英]Replace a String between two Strings

Let's say we have something like: 假设我们有类似的东西:

&firstString=someText&endString=OtherText

And I would like to replace "someText" with something else. 我想用其他东西替换“someText”。 What is the best way to do this considering the fact that I do not know what someText might be (any string) and all I know is that it will be surrounded with &firstString= and &endString= 考虑到我不知道someText可能是什么(任何字符串)这一事实,最好的方法是什么,我所知道的是它将被&firstString =和&endString =包围

Edit: sorry looks like this is not clear enough. 编辑:对不起看起来这还不够清楚。 I do not know what "someText" might be, the only information I have is that it will be between &firstString= and &endString= 我不知道“someText”可能是什么,我唯一的信息是它将介于&firstString =和&endString =之间

I was thinking about using split multiple times but it sounded ugly .. 我正在考虑多次使用拆分,但听起来很难看。

您可以使用支持正则表达式的String#replaceAll ,如下所示:

String newstr = str.replaceAll("(&firstString=)[^&]*(&endString=)", "$1foo$2");

The easiest to understand way to do it is to search for the delimiters, and cut out a substring between their positions, like this: 最容易理解的方法是搜索分隔符,并在它们的位置之间剪切一个子字符串,如下所示:

String str = "&firstString=someText&endString=OtherText";
String firstDelim = "&firstString=";
int p1 = str.indexOf(firstDelim);
String lastDelim = "&endString=";
int p2 = str.indexOf(lastDelim, p1);   // look after start delimiter    
String replacement = "quick_brown_fox";
if (p1 >= 0 && p2 > p1) {
    String res = str.substring(0, p1+firstDelim.length())
               + replacement
               + str.substring(p2);
    System.out.println(res);
}
yourString.replace("someText", "somethingElse");

EDIT based on clarification 编辑基于澄清

String x = "&firstString=someText&endString=OtherText";
int firstPos = x.indexOf("&firstString=") + "&firstString=".length();
int lastPos = x.indexOf("&endString", firstPos);
String y = x.substring(0,firstPos) + "Your new text" + x.substring(lastPos);
System.out.println(y);

Output: 输出:

&firstString=Your new text&endString=OtherText

As I understood the question, you should do something like this, but I'm not sure I totally got what you asked: 当我理解这个问题时,你应该做这样的事情,但我不确定我是否完全得到了你的要求:

yourString = firstString + replacingString + endString;

If you want the "replaced" string: 如果你想要“替换”字符串:

replacedString = wholeString.substring(0, wholeString.lastIndexOf(endString) - 1);
replacedString = tempString.substring(firstString.length() + 1);

This solution checks that we're not going of the end of the string and will do it for multiple occurrences. 这个解决方案检查我们是不是在字符串的末尾,并且会在多次出现时执行此操作。

 int startIndex = text.indexOf(startDelim);
 while (startIndex > -1)
 {
    int endIndex = text.indexOf(endDelim, startIndex);
    String endPart = "";

    if ((endIndex + endDelim.length()) < text.length())
       endPart = text.substring(endIndex + endDelim.length());

    text = text.substring(0, startIndex) + replacementText + endPart;
    startIndex = text.indexOf(startDelim);
 }

只是替换整个&firstString=someText&因为这包括我认为与URL相关的外部障碍?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM