StringBuilder删除功能 - 如何删除单个字符? [英] StringBuilder Remove function - How to remove single character?

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

问题描述

我需要循环一个字符串并删除除数字或

字母之外的所有字符。我收到了一个ArgumentOutOfRangeException:索引超出了

范围。必须是非负的且小于集合的大小


不确定发生了什么......欢迎任何建议!


public static string fixStr(string aStr)

{

StringBuilder bStr = new StringBuilder(aStr.Length);

for(int i = 0; i< aStr.Length; i ++)

{

if(!Char.IsDigit(aStr [i])|!Char.IsLetter(aStr [i] ))

{

bStr.Remove(i,1);

}

}

返回bStr.ToString();

}

I need to loop through a string and remove all characters except numbers or
letters. I am getting an ArgumentOutOfRangeException: "Index was out of
range. Must be non-negative and less than the size of the collection"

Not sure what is going on... any suggestions welcome!

public static string fixStr(string aStr)
{
StringBuilder bStr = new StringBuilder(aStr.Length);
for (int i = 0; i < aStr.Length; i++)
{
if (!Char.IsDigit(aStr[i]) | !Char.IsLetter(aStr[i]))
{
bStr.Remove(i,1);
}
}
return bStr.ToString();
}

推荐答案



" ;杰科" < www.clearpointsystems.com@use_contact_form.com>在消息中写道

新闻:Uw ****************** @ newssvr13.news.prodigy.c om ...

"deko" <www.clearpointsystems.com@use_contact_form.com> wrote in message
news:Uw******************@newssvr13.news.prodigy.c om...
我需要循环一个字符串并删除除数字或
字母之外的所有字符。我收到了一个ArgumentOutOfRangeException:索引超出了
范围。必须是非负的且小于集合的大小

不确定发生了什么......欢迎任何建议!

public static string fixStr(string aStr )
{StringBuilder bStr = new StringBuilder(aStr.Length);
for(int i = 0; i< aStr.Length; i ++)
{
if(!Char.IsDigit(aStr [i])|!Char.IsLetter(aStr [i]))
{
bStr.Remove(i,1);
}
}
返回bStr.ToString();
}
I need to loop through a string and remove all characters except numbers or
letters. I am getting an ArgumentOutOfRangeException: "Index was out of
range. Must be non-negative and less than the size of the collection"

Not sure what is going on... any suggestions welcome!

public static string fixStr(string aStr)
{
StringBuilder bStr = new StringBuilder(aStr.Length);
for (int i = 0; i < aStr.Length; i++)
{
if (!Char.IsDigit(aStr[i]) | !Char.IsLetter(aStr[i]))
{
bStr.Remove(i,1);
}
}
return bStr.ToString();
}




您的StringBuilder为空。你不妨只是附上数字和

字母


public static string fixStr(string aStr)

{

StringBuilder bStr = new StringBuilder(aStr.Length);

for(int i = 0; i< aStr.Length; i ++)

{

char c = aStr [i];

if(Char.IsDigit(c)| Char.IsLetter(c))

{

bStr.Append(c);

}

}

返回bStr.ToString();

}


David



Your StringBuilder is empty. You might as well just append the numbers and
letters

public static string fixStr(string aStr)
{
StringBuilder bStr = new StringBuilder(aStr.Length);
for (int i = 0; i < aStr.Length; i++)
{
char c = aStr[i];
if (Char.IsDigit(c) | Char.IsLetter(c))
{
bStr.Append(c);
}
}
return bStr.ToString();
}

David


Deko,

除了David''注释我会将字符串本身传递给

StringBuilder的构造函数而不是长度。这会将

StringBuilder初始化为字符串的值,而不是将

容量设置为字符串的长度。


类似于:
Deko,
In addition to David''s comments I would pass the string itself to
StringBuilder''s constructor instead of the length. This will initialize the
StringBuilder to the value of the string, instead of just setting the
capacity to the length of the string.

Something like:
public static string fixStr(string aStr)
{


StringBuilder bStr = new StringBuilder(aStr );

for(int i = 0; i< aStr.Length; i ++)
{
if(!Char.IsDigit(aStr [i])|!Char .IsLetter(aStr [i]))
{
bStr.Remove(i,1);
}
}
返回bStr.ToString();
}


希望这有帮助

Jay


" deko" < www.clearpointsystems.com@use_contact_form.com>在消息中写道

新闻:Uw ****************** @ newssvr13.news.prodigy.c om ...我需要循环一个字符串并删除除数字或
字母之外的所有字符。我收到了一个ArgumentOutOfRangeException:索引超出了
范围。必须是非负的且小于集合的大小

不确定发生了什么......欢迎任何建议!

public static string fixStr(string aStr )
{StringBuilder bStr = new StringBuilder(aStr.Length);
for(int i = 0; i< aStr.Length; i ++)
{
if(!Char.IsDigit(aStr [i])|!Char.IsLetter(aStr [i]))
{
bStr.Remove(i,1);
}
}
返回bStr.ToString();
}
public static string fixStr(string aStr)
{
StringBuilder bStr = new StringBuilder(aStr);
for (int i = 0; i < aStr.Length; i++)
{
if (!Char.IsDigit(aStr[i]) | !Char.IsLetter(aStr[i]))
{
bStr.Remove(i,1);
}
}
return bStr.ToString();
}
Hope this helps
Jay

"deko" <www.clearpointsystems.com@use_contact_form.com> wrote in message
news:Uw******************@newssvr13.news.prodigy.c om...I need to loop through a string and remove all characters except numbers or
letters. I am getting an ArgumentOutOfRangeException: "Index was out of
range. Must be non-negative and less than the size of the collection"

Not sure what is going on... any suggestions welcome!

public static string fixStr(string aStr)
{
StringBuilder bStr = new StringBuilder(aStr.Length);
for (int i = 0; i < aStr.Length; i++)
{
if (!Char.IsDigit(aStr[i]) | !Char.IsLetter(aStr[i]))
{
bStr.Remove(i,1);
}
}
return bStr.ToString();
}





" ; Jay B. Harlow [MVP - Outlook]" < JA ************ @ msn.com>写在消息

新闻:uI ************** @ tk2msftngp13.phx.gbl ...

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uI**************@tk2msftngp13.phx.gbl...
Deko,除了David的评论之外,我还会将字符串本身传递给StringBuilder的构造函数而不是长度。这会将StringBuilder初始化为字符串的值,而不是仅仅将
容量设置为字符串的长度。
Deko,
In addition to David''s comments I would pass the string itself to
StringBuilder''s constructor instead of the length. This will initialize
the StringBuilder to the value of the string, instead of just setting the
capacity to the length of the string.




但是StringBuilder.Remove必须复制

删除的所有后续char,而StringBuilder.Append(char)只需要预先分配

StringBuilder设置现有字符的值并增加

的长度。


David



But StringBuilder.Remove has to copy all the subsequent char''s over the
removed ones, while StringBuilder.Append(char) with a pre-allocated
StringBuilder only has to set the value of an existing char and increment
the length.

David


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

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