Java程序,用于删除指定位置的字符
要删除指定位置的字符,请使用以下逻辑-
假设以下是我们的字符串。
String str = "山屋的困扰!";
我们要删除位置7处的字符。为此,请使用以下逻辑。
// removing character at position 7 int pos = 7; String res = str.substring(0, pos) + str.substring(pos + 1);
以下是带有输出的完整示例。
示例
public class Demo { public static void main(String[] args) { String str = "山屋的困扰!"; System.out.println("String: "+str); // removing character at position 7 int pos = 7; String res = str.substring(0, pos) + str.substring(pos + 1); System.out.println("String after removing a character: "+res); }}输出结果
String: 山屋的困扰! String after removing a character: The Hauting of Hill House!
热门推荐
10 高一祝福语简短 励志
11 立秋节气简短祝福语
12 简短公司周年祝福语大全
13 中考感言简短祝福语大全
14 体育老师简短祝福语
15 干杯生日祝福语简短独特
16 特别简短的祝福语老师
17 朋友坐车走了祝福语简短
18 给英雄的祝福语简短