java去掉末尾append("OR ")拼接的or
时间: 2023-07-19 15:50:50 浏览: 67
如果您想要将一个字符串中所有以 "OR " 结尾的子串去掉,可以使用以下代码:
```java
String str = "apple OR banana OR orange OR ";
String[] substrs = str.split("OR ");
StringBuilder sb = new StringBuilder();
for (String substr : substrs) {
if (!substr.isEmpty()) {
sb.append(substr);
}
}
String result = sb.toString();
System.out.println(result);
```
输出:
```
applebananorange
```
如果您想要保留最后一个 "OR " 后面的内容,可以使用以下代码:
```java
String str = "apple OR banana OR orange OR ";
String[] substrs = str.split("OR ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < substrs.length - 1; i++) {
if (!substrs[i].isEmpty()) {
sb.append(substrs[i]);
}
}
String result = sb.toString() + "OR " + substrs[substrs.length - 1];
System.out.println(result);
```
输出:
```
applebananaorange OR
```
阅读全文