Why StringBuilder's delete shifts string to the left and does not delete the last character?

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

Why StringBuilder's delete shifts string to the left and does not delete the last character?

问题

我在这里是新手。
我正在尝试弄清楚如何删除字符串中的最后一个字符,但我没有成功。
我使用我的代码 - 使用 StringBuilder。

例如,我有一个字符串 'abcde',
我想在删除第一个字符后得到字符串 bcde,
但我得到了字符串 bcdee。
每次我不断删除 - 最后一个字符没有被删除。
我想删除字符串中的第一个字符,并在末尾什么也不留下。

		StringBuilder test = new StringBuilder();
		test.append("abcde");
		test.deleteCharAt(0);
		test.deleteCharAt(0);
		test.deleteCharAt(0);
		test.deleteCharAt(0);

运行此代码后,
我得到:我想得到:

bcdee    bcde
cdeee    cde
deeee    de
eeeee    e
英文:

I'm new here.
I'm trying to figure out how to delete the last character in the string but I'm not succeeding.
I use my code - in StringBuilder.

For example I have a string of 'abcde'
i want to get after delete the first character - the String bcde
but i got the string bcdee.
All the time I keep deleting - the last character is not deleted
I want to delete the first character in the string and leave nothing at the end.

		StringBuilder test = new StringBuilder();
		test.append("abcde");
		test.deleteCharAt(0);
		test.deleteCharAt(0);
		test.deleteCharAt(0);
		test.deleteCharAt(0);

After running this code
I get: I want to get:

bcdee    bcde
cdeee    cde
deeee    de
eeeee    e

答案1

得分: 1

> 我正在尝试弄清楚如何删除字符串中的最后一个字符。

要删除字符串中的最后一个字符:

    String s = someString.substring(0, someString.length() - 1);

请注意,String 是不可变的,所以上述实际上是在创建一个新的 String。

要删除 StringBuilder 中的最后一个字符:

    someStringBuilder.setLength(someStringBuilder.length() - 1);

您还可以使用以下方法删除最后一个字符:

    someStringBuilder.deleteCharAt(someStringBuilder.length() - 1);

上述代码片段都假定确实存在要删除的最后一个字符。

-------------------

您的代码调用了 deleteCharAt(0)。那会删除第一个字符,而不是最后一个字符。

---------------------

但是您的示例似乎要求您删除字符串末尾的重复字符。为此,您需要使用循环:

    伪代码

    当最后一个字符等于倒数第二个字符时:
        删除最后一个字符

看看您是否能够自己编写出这个逻辑...
英文:

> I'm trying to figure out how to delete the last character in the string.

To delete the last character in a String:

String s = someString.substring(0, someString.length() - 1)

Note that a String is ummutable, so the above is actually creating a new String.

To remove the last character in a StringBuilder:

someStringBuilder.setLength(someStringBuilder.length() - 1);

You can also remove the last character using

someStringBuilder.deleteCharAt(someStringBuilder.length() - 1);

The above code fragments all assume that there is a last character to be removed.


Your code calls deleteCharAt(0). That deletes the first character not the last one.


But your example seems to require you to remove duplicate characters at the end of a string. To do that, you need a loop:

pseudocode

while last character equals second to last character:
    remove last character

See if you can code that for yourself ...

答案2

得分: 0

首先,您的代码:

    StringBuilder test = new StringBuilder();
    test.append("abcde");
    System.out.println(test);
    test.deleteCharAt(0);
    System.out.println(test);
    test.deleteCharAt(0);
    System.out.println(test);
    test.deleteCharAt(0);
    System.out.println(test);
    test.deleteCharAt(0);
    System.out.println(test);

输出:

abcde
bcde
cde
de

这是正确的,与您在原始问题中提到的一样,没有额外的 e。因此,请尝试找出您的代码中额外引起此问题的部分。


为什么在 StringBuilder 中的 delete 操作会将字符串向左移并且不会删除最后一个字符?

它会向左移,因为通过 test.deleteCharAt(0);,您明确告诉 Java 删除特定索引(在这种情况下为 0)处的字符,因此新的字符串将不以空字符开头;然而,它不会移除最后一个字符,并且最后的额外字符实际上根本未被添加(没有额外的 e)。


除了 .setLength().subString() 方法外,您也可以简单地使用相同的 .deleteCharAt(int) 方法,操作如下:

    StringBuilder test = new StringBuilder();
    test.append("abcde");
    System.out.println(test);
    test.deleteCharAt(test.length()-1);
    System.out.println(test);
    test.deleteCharAt(test.length()-1);
    System.out.println(test);
    test.deleteCharAt(test.length()-1);
    System.out.println(test);
    test.deleteCharAt(test.length()-1);
    System.out.println(test);
英文:

First of all, your code:

    StringBuilder test = new StringBuilder();
    test.append("abcde");
    System.out.println(test);
    test.deleteCharAt(0);
    System.out.println(test);
    test.deleteCharAt(0);
    System.out.println(test);
    test.deleteCharAt(0);
    System.out.println(test);
    test.deleteCharAt(0);
    System.out.println(test);

outputs:

abcde
bcde
cde
de

which is correct, as expected, without any additional es, as you mention it in the original question. So, try to figure out what you have additionally in your code that causes this.


>Why delete in StringBuilder shift string to left and doesn't delete the last character?

It shifts, because, by test.deleteCharAt(0);, you explicitly tell Java to delete Char at specific (0, in this case) index, hence the new String will not be starting with an empty character; however, it should nt and it does not remove the last character, and the last extra character is simply never appended (there is no extra e).


Apart from the .setLength() and .subString(), you can simply use the same .deleteCharAt(int) API and do as follows:

    StringBuilder test = new StringBuilder();
    test.append("abcde");
    System.out.println(test);
    test.deleteCharAt(test.length()-1);
    System.out.println(test);
    test.deleteCharAt(test.length()-1);
    System.out.println(test);
    test.deleteCharAt(test.length()-1);
    System.out.println(test);
    test.deleteCharAt(test.length()-1);
    System.out.println(test);

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

发表评论

匿名网友

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

确定