如何从String中删除单个字符 [英] How to remove single character from a String

查看:244
本文介绍了如何从String中删除单个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了访问Java中String的单个字符,我们有 String.charAt(2)。是否有任何内置函数可以删除java中String的单个字符?

For accessing individual characters of a String in Java, we have String.charAt(2). Is there any inbuilt function to remove an individual character of a String in java?

这样的事情:

if(String.charAt(1) == String.charAt(2){
   //I want to remove the individual character at index 2. 
}


推荐答案

您还可以使用可变的 StringBuilder 类。

You can also use the StringBuilder class which is mutable.

StringBuilder sb = new StringBuilder(inputString);

它的方法有 deleteCharAt() ,以及许多其他mutator方法。

It has the method deleteCharAt(), along with many other mutator methods.

只需删除您需要的字符删除然后得到如下结果:

Just delete the characters that you need to delete and then get the result as follows:

String resultString = sb.toString();

这可以避免创建不必要的字符串对象。

This avoids creation of unnecessary string objects.

这篇关于如何从String中删除单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆