如何替换所有不位于两个字符之间的字符串?

huangapple go评论78阅读模式
英文:

How do you replace all strings that are not between two characters?

问题

以下是要翻译的内容:

在之前:
text(text)text

之后:
...(text)...

英文:

Example (replacing 'text' with '...'):

Before:
text(text)text

After:
...(text)...

答案1

得分: 1

在这种情况下,更容易找到你想保留的内容,并替换其余部分。

例如,像这样:

static String abbreviate(String input, String openTag, String closeTag) {
    String regex = Pattern.quote(openTag) + ".*?" + Pattern.quote(closeTag);
    StringBuilder buf = new StringBuilder();
    int start = 0;
    for (Matcher m = Pattern.compile(regex).matcher(input); m.find(); start = m.end()) {
        if (start < m.start())
            buf.append("...");
        buf.append(m.group());
    }
    if (start < input.length())
        buf.append("...");
    return buf.toString();
}

测试

System.out.println(abbreviate("text(text)text(text)text", "(", ")"));
System.out.println(abbreviate("text$text$text$text$text$text", "$", "$"));
System.out.println(abbreviate("text(text)text", ")", "("));

输出

...(text)...(text)...
...$text$...$text$...
...
英文:

In this case it is easier to find what you want to keep, and replace the rest.

E.g. like this:

static String abbreviate(String input, String openTag, String closeTag) {
	String regex = Pattern.quote(openTag) + &quot;.*?&quot; + Pattern.quote(closeTag);
	StringBuilder buf = new StringBuilder();
	int start = 0;
	for (Matcher m = Pattern.compile(regex).matcher(input); m.find(); start = m.end()) {
		if (start &lt; m.start())
			buf.append(&quot;...&quot;);
		buf.append(m.group());
	}
	if (start &lt; input.length())
		buf.append(&quot;...&quot;);
	return buf.toString();
}

Test

System.out.println(abbreviate(&quot;text(text)text(text)text&quot;, &quot;(&quot;, &quot;)&quot;));
System.out.println(abbreviate(&quot;text$text$text$text$text$text&quot;, &quot;$&quot;, &quot;$&quot;));
System.out.println(abbreviate(&quot;text(text)text&quot;, &quot;)&quot;, &quot;(&quot;));

Output

...(text)...(text)...
...$text$...$text$...
...

答案2

得分: 0

需要遍历字符并且只追加那些位于两个指定字符之间的字符可以按照以下方式完成
```java
private String splitStr(String str, char first, char second) {
    StringBuilder sb = new StringBuilder();
    if(str.isEmpty() || str.indexOf(first) < 0 || str.indexOf(second) < 0)
        return sb.toString();
    char[] chars = str.toCharArray();
    boolean append = false;
    for(char c : chars) {
        if(c == first) {
            sb.append(c);
            append = true;
        }else if(c == second) {
            sb.append(c);
            append = false;
        }else if(append)
            sb.append(c);
    }
    return sb.toString();
}

一些示例案例:

"text(text)text(text)text(text)text" => "(text)(text)(text)"
"text(text)text" => "(text)"
英文:

You need to iterate over the characters and only append those that are between the two specified characters. This can be done as follows:

private String splitStr(String str, char first, char second) {
		StringBuilder sb = new StringBuilder();
		if(str.isEmpty() || str.indexOf(first) &lt; 0 || str.indexOf(second) &lt; 0)
			return sb.toString();
		char[] chars = str.toCharArray();
		boolean append = false;
		for(char c : chars) {
			if(c == first) {
				sb.append(c);
				append = true;
			}else if(c == second) {
				sb.append(c);
				append = false;
			}else if(append)
				sb.append(c);
		}
		return sb.toString();
}

Some sample cases are:

&quot;text(text)text(text)text(text)text&quot; =&gt; &quot;(text)(text)(text)&quot;
&quot;text(text)text&quot; =&gt; &quot;(text)&quot;

答案3

得分: -1

String s = "text(text)text";
String newString = s.replace("text", "...");
System.out.println(newString);      //returns ...(...)...
  1. 注意,"(text)" 仍然包含 "text",括号不会阻止其被替换。

  2. 你需要将结果赋值给一个新的字符串才能使用它。字符串是不可变的


<details>
<summary>英文:</summary>

String s = "text(text)text";
String newString = s.replace("text", "...");
System.out.println(newString); //returns ...(...)...


1. Note that &quot;(text)&quot; still contains &quot;text&quot;, the braces around it will not stop it from being replaced.

2. You need to assign the result to a new String to use it. Strings are [immutable][1]


  [1]: https://www.javatpoint.com/immutable-string

</details>



huangapple
  • 本文由 发表于 2020年4月8日 04:41:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61089066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定