如何删除字符串中的字符? [英] How to delete characters of a string?

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

问题描述

您好,我想知道如何删除字符串的字符,例如,删除该字符串的字符0和4.

Hello, i want know how can i delete characters of an string, for example, delete the characters 0 and 4 of this string.

string frase  = "Ayer estaba solo";


最好的问候
ÁngelManuel


Best regards
Ángel Manuel

推荐答案

要删除0,请尝试以下操作-

To delete 0 try something like this -

while(frase.indexof(0)!=-1)
{
frase.Remove(frase.indexOf(0));
}



您可以参数化此方法以传递所有值.



You can paramterize this method to pass all values.


方法如下:

Here is how:

string frase  = "Ayer estaba solo";
System.Text.StringBuilder sb = new System.Text.StringBuilder(frase);
sb.Remove(1, 4);
frase = sb.ToString();



使用StringBuilder来提高速度很重要,即使简单的串联也比使用字符串快.方法string.Format的速度也快于等效字符串的连接.

注意! OP将标签从C#更改为C ++.我对此不承担任何责任.当标签为C#时,我的回答是在C#中.请不要以此为基础进行否决.



—SA



It''s important to use StringBuilder for speed, even simple concatenations are faster then with string. The method string.Format is also faster then string equivalent concatenations.

Attention! OP changed a tag from C# to C++. I cannot be hold responsible for that. My answer was in C# when the tag was C#. Please do not down-vote based on that.



—SA


使用擦除功能:从指定位置删除字符串中的一个元素或一系列元素.

字符串frase ="Ayer estaba solo";
frase.erase(0,1);
第一个参数是
_Pos:要删除的字符串中第一个字符的索引.
_Count:如果在以_Pos开头的字符串范围内,则删除的元素数.
Use erase function : Removes an element or a range of elements in a string from a specified position.

string frase = "Ayer estaba solo";
frase.erase(0,1);
First param is
_Pos: The index of the first character in the string to be removed.
_Count: The number of elements that will be removed if there are as many in the range of the string beginning with _Pos.


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

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