Java 删除字符串中某个位置的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20304671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:57:57  来源:igfitidea点击:

Delete char at position in string

javastring

提问by Meh

Currently, I am working on a project that requires to delete a char at a set position in a string. Is there a simple way to do this?

目前,我正在处理一个需要删除字符串中某个设置位置的字符的项目。有没有一种简单的方法可以做到这一点?

回答by Konstantin Yovkov

Strings are immutable! But to accomplish your task, you can

字符串是不可变的!但是为了完成你的任务,你可以

  1. Copy the substring from the start of the string to the character that has to be deleted
  2. Copy the substring from the character that has to be deleted to the end of the String.
  3. Append the second String to the first one.
  1. 将字符串开头的子字符串复制到要删除的字符
  2. 将子字符串从要删除的字符复制到字符串的末尾。
  3. 将第二个字符串附加到第一个字符串。

For example, let's say you have to remove the third character:

例如,假设您必须删除第三个字符:

String input = "Hello, World";
String first = input.substring(0, 3);
String second = input.substring(4);
String result = first + second;

回答by Vineet Kosaraju

The way you would do this is by copying the oldstring into a newstring, being careful to remove the one character, like so:

这样做的方法是将旧字符串复制到新字符串中,小心删除一个字符,如下所示:

String newString = oldString.substring(0, index) + oldString.substring(index+1);

Keep in mind that if you are doing this several times, constantly making new strings is rather inefficient.

请记住,如果您多次执行此操作,则不断制作新字符串的效率相当低。

回答by Alex Vulaj

Use a StringBuilder, which has the method deleteCharAt(). Then, you can just use stringBuilder.toString()to get your string.

使用具有方法的 StringBuilder deleteCharAt()。然后,您可以使用stringBuilder.toString()来获取您的字符串。

EDIT, here's an example:

编辑,这是一个例子:

public static void main(String[] args) {
    String string = "bla*h";
    StringBuilder sb = new StringBuilder(string);
    sb.deleteCharAt(3);
    // Prints out "blah"
    System.out.println(sb.toString());
}

回答by WelcomeTo

You can convert your string to StringBuilder, which have convinient deletaCharAtmethod.

您可以将字符串转换为StringBuilder,它具有方便的deletaCharAt方法。

String strWithoutChar = new StringBuilder(yourString).deleteCharAt(yourIndex).toString();

But keep in mind, this gives to you ability to remove some boilerplate code, but it takes more memory in compassion to using String#substringmethod.

但请记住,这使您能够删除一些样板代码,但出于同情使用String#substring方法需要更多内存。

回答by Sameer Sawla

Whatever is being said here by fellow scholars, I agree to them that it gives the correct answer. But if you are dealing with a production level program where every loophole gets magnified, I would suggest you to not use "Substring" method of string because:

无论同行学者在这里说什么,我都同意他们的意见,它给出了正确的答案。但是,如果您正在处理每个漏洞都被放大的生产级程序,我建议您不要使用字符串的“Substring”方法,因为:

(1). substring method actually leaves the parent string unflagged for garbage collector. Due to this garbage collector cannot clean it up in its process, the heap space gets occupied and unused parent strings still remains to exist in the heap space even when it is\ not in use.

(1). substring 方法实际上不为垃圾收集器标记父字符串。由于这个垃圾收集器无法在其进程中清理它,堆空间被占用,即使在未使用时,未使用的父字符串仍然存在于堆空间中。

(2). If you deal with a number of string manipulations such as these, it may eventually result in 'Out of Heap Memory' exception which is not desirable at any point in time in the program.

(2). 如果您处理诸如此类的大量字符串操作,最终可能会导致“堆内存不足”异常,这在程序中的任何时间点都是不可取的。

Use char array and its manipulations to achieve what you want :

使用 char 数组及其操作来实现您想要的:

public static void fn1()
{
 String a = "Stack Overflow";
 char[] b = a.toCharArray();
 j // index position of the character that you want to delete
 for(int i=j;i<b.length-1;i++)
 {
     b[i]=b[i+1];
 }
 b[b.length-1]='
String removeCharAt(String s, int index) {
    return s.substring(0, index).concat(s.substring(index + 1));
}
0'; // '##代码##0' is the null character System.out.println(new String(b)); }

回答by Jon Erickson

An alternative is to use the substring method but using concat instead of +

另一种方法是使用 substring 方法,但使用 concat 而不是 +

##代码##