英文:
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) + ".*?" + 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();
}
Test
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", ")", "("));
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) < 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();
}
Some sample cases are:
"text(text)text(text)text(text)text" => "(text)(text)(text)"
"text(text)text" => "(text)"
答案3
得分: -1
String s = "text(text)text";
String newString = s.replace("text", "...");
System.out.println(newString); //returns ...(...)...
-
注意,"(text)" 仍然包含 "text",括号不会阻止其被替换。
-
你需要将结果赋值给一个新的字符串才能使用它。字符串是不可变的。
<details>
<summary>英文:</summary>
String s = "text(text)text";
String newString = s.replace("text", "...");
System.out.println(newString); //returns ...(...)...
1. Note that "(text)" still contains "text", 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论