[英]Delete every occurence of a character from a String
I have the below program that I have written to delete every character from a String. 我有下面编写的程序,用于删除字符串中的每个字符。
public static String removeMultipleCharsFromStr(String str,char temp) {
StringBuffer sb = new StringBuffer();
for(int i=0;i<str.length();i++) {
sb.append(str.charAt(i));
}
for(int i=0;i<sb.length();i++) {
if(Character.toLowerCase(sb.charAt(i)) == Character.toLowerCase(temp)) {
sb.deleteCharAt(i);
}
}
System.out.println("Final string: " +sb.toString());
return sb.toString();
}
When I invoke the method, it doesn't remove every occurrence of the character though. 当我调用该方法时,它并不会删除每次出现的字符。 Can someone please tell me what mistake am I making here? 有人可以告诉我我在这里犯什么错误吗?
public class RemoveChar {
public static void main(String[] args) {
removeMultipleCharsFromStr("SSSSSaerty", 'S');
}
Outputs - Final string: SSaerty
输出- Final string: SSaerty
Please advise. 请指教。 Also, I understand there can be a hundred other solutions to my question. 另外,我知道我的问题还有其他一百种解决方案。 I'd appreciate if you could let me know the mistakes in my code rather than suggesting your solution ;) 如果您能让我知道我的代码中的错误,而不是提出解决方案,我将不胜感激;)
Not sure what happened to the other answer, but let's see what happens in your algorithm. 不确定另一个答案发生了什么,但是让我们看看您的算法中发生了什么。
When everything is placed in the StringBuffer
it will look something like this: 将所有内容放置在StringBuffer
,它将看起来像这样:
i
Index: 0 1 2 3 4 5 6 7 8 9
Value: S S S S S a e r t y
In your first iteration (in the last loop) you are looking at index 0 ( i
is 0). 在第一次迭代中(在最后一个循环中),您正在查看索引0( i
为0)。 The char at index 0 is S , so you remove it. 索引0处的字符为S ,因此将其删除。 Now your buffer looks like this: 现在您的缓冲区如下所示:
i
Index: 0 1 2 3 4 5 6 7 8
Value: S S S S a e r t y
The first S is gone, but instead of leaving index 0 empty, all the other chars moved 1 place to the left. 第一个S消失了,但是没有将索引0留空,所有其他字符向左移动了1个位置。 In your next iteration i
is 1. This means that you never look at the new value at index 0. 在下一次迭代中, i
为1。这意味着您永远不会查看索引0处的新值。
The same thing happens when you go from 1 to 2 and 2 to 3. In the end that means you have skipped 2 S values. 当您从1转到2,从2转到3时,也会发生同样的事情。最后,这意味着您已跳过2 S值。 If you increase the number of continuous S values, you will see more of them in the output. 如果增加连续S值的数量,则会在输出中看到更多的值。
i
Index: 0 1 2 3 4 5 6 7
Value: S S S a e r t y
i
Index: 0 1 2 3 4 5 6
Value: S S a e r t y <-- See?
I'd appreciate if you could let me know the mistakes in my code rather than suggesting your solution ;) 如果您能让我知道我的代码中的错误,而不是提出解决方案,我将不胜感激;)
Fair enough. 很公平。 Good luck with it. 祝你好运。 :) :)
The for-loop advances while you're deleting a character. 删除字符时,for循环会前进。 So another character will be on the index position that you've just passed. 因此,另一个字符将位于您刚刚通过的索引位置。 An fix would be: 解决方法是:
public static String removeMultipleCharsFromStr(String str,char temp) {
StringBuffer sb = new StringBuffer();
for(int i=0;i<str.length();i++) {
sb.append(str.charAt(i));
}
for(int i=0;i<sb.length();i++) {
if(Character.toLowerCase(sb.charAt(i)) == Character.toLowerCase(temp)) {
sb.deleteCharAt(i);
i--;
}
}
System.out.println("Final string: " +sb.toString());
return sb.toString();
}
Although you might consider using the standard replacement methods as suggested in the comments: 尽管您可以考虑使用注释中建议的标准替换方法:
str.replaceAll(String.valueOf(temp),"")
Just add i--;
只需添加我- i--;
after sb.deleteCharAt(i);
在sb.deleteCharAt(i);
. 。 This way, you continue where you left off. 这样,您可以从上次中断的地方继续。 Otherwise, you skip the next char because the size of your StringBuffer
reduces by 1. 否则,您将跳过下一个字符,因为StringBuffer
的大小减小了1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.