编写方法String delStar(String s),删除指定字符串中末尾的*号,例如delStar(”**** A*BC* DEF*G *******")的结果是”**** A*BC*DEF*G"。
时间: 2023-09-04 07:09:42 浏览: 74
好的,我明白您的问题。以下是一个可以实现您要求的Java代码:
```
public class Main {
public static void main(String[] args) {
String s = "**** A*BC* DEF*G *******";
String result = delStar(s);
System.out.println(result);
}
public static String delStar(String s) {
StringBuilder sb = new StringBuilder(s.trim());
int count = 0;
for (int i = sb.length() - 1; i >= 0; i--) {
if (sb.charAt(i) == '*') {
count++;
} else {
break;
}
}
sb.delete(sb.length() - count, sb.length());
return sb.toString();
}
}
```
这个方法首先使用`trim()`函数去掉指定字符串开头和末尾的空格,然后从字符串末尾开始遍历,找到末尾的`*`号的数量`count`。最后,将字符串末尾的`*`号删除,并返回新的字符串。
希望能够帮助到您!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)