当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java StringBuffer deleteCharAt(int index)用法及代码示例


Java StringBuffer 类的deleteCharAt(int index) 方法用于删除该序列指定位置的字符。删除一个字符后,当前序列缩短一个字符。

用法:

public StringBuffer deleteCharAt(int index)

参数:

数据类型 参数 描述
int index 索引是要删除的字符的位置。

返回值:

此 deleteCharAt(int index) 方法返回此对象。

异常:

StringIndexOutOfBoundsException - 如果索引为负或大于或等于字符串缓冲区的 length()。

兼容版本:

Java 1.2 及以上

例子1

public class StringBufferDeleteCharAtExample1 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("javatpoint");
	    System.out.println("string1:" + sb);
	    // deleting the character at index 4
	    sb = sb.deleteCharAt(4);
	    System.out.println("After deleting:" + sb);
	   
	    sb = new StringBuffer("hello java");
	    System.out.println("string2:" + sb);
	    // deleting the character at index 5
	    sb = sb.deleteCharAt(5);
	    System.out.println("After deleting:" + sb);
	}
}

输出:

string1:javatpoint
After deleting:javapoint
string2:hello java
After deleting:hellojava

例子2

此 deleteCharAt(int) 方法在提供负索引值时抛出异常。

public class StringBufferDeleteCharAtExample2 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("javatpoint");
	    System.out.println("string1:" + sb);
	    // try to delete the character at index value negative
	    sb = sb.deleteCharAt(-1);
	    System.out.println("After deleting:" + sb);
	}
}

输出:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException:String index out of range:-1
	at java.lang.AbstractStringBuilder.deleteCharAt(Unknown Source)
	at java.lang.StringBuffer.deleteCharAt(Unknown Source)
	at StringBufferDeleteCharAtExample2.main(StringBufferDeleteCharAtExample2.java:6)
string1 = javatpoint

例子3

删除 Character.charCount(thisSequence.codePointAt(index)) 方法的索引值处的字符。

public class StringBufferDeleteCharAtExample3 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("javatpoint");
	    System.out.println("string:" + sb);
	    // deleting string buffer using character count at code point
	    int cp = sb.codePointAt(0);
	    System.out.println("codepoint at 0:"+cp);
	    int totalCharacter =Character.charCount(cp);
	    System.out.println("total character at codepoint "+cp+":" + totalCharacter);
	    sb = sb.deleteCharAt(totalCharacter);
	    System.out.println("after deleting at index "+totalCharacter +":"+ sb);
	}
}

输出:

string:javatpoint
codepoint at 0:106
total character at codepoint 106:1
after deleting at index 1:jvatpoint

示例 4

在这个例子中,我们从用户那里获取输入字符串和字符索引。

import java.util.Scanner;

public class StringBufferDeleteCharAtExample4 {
	public static void main(String[] args) {
	    StringBuffer sb = new StringBuffer("");
	    
	    System.out.print("enter your string value:");
	    Scanner sc = new Scanner(System.in);
	    sb.append(sc.nextLine());
	    
	    System.out.print("enter index:");
        int index = sc.nextInt(); 
	   
        // deleting the character at input index
        System.out.println("after deleting resultant string:"+sb.deleteCharAt(index));
	    sc.close();
	}
}

输出:

enter your string value:hello string
enter index:3
after deleting resultant string:helo string






相关用法


注:本文由纯净天空筛选整理自 Java StringBuffer deleteCharAt(int index) method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。