String Builder插入 [英] String Builder insert

查看:97
本文介绍了String Builder插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行sb.insert时,10个空格指定的字符串会增长。

有办法阻止字符串增长。它变成14空格len

test.file


我喜欢能够插入第3个位置但是长度应该保持10个


string initialValue =" " ;;

string xyz =" xyz";

sb = new StringBuilder(initialValue);

sb.Insert(3,xyz );

m.Writer(" c:/test.txt" ;, sb.ToString());

10 len space designated strings grows when i do sb.insert.
is there way to stop string growing. It becomes 14 space len on
test.file

I like to be able insert 3rd position but length should stay 10

string initialValue = " ";
string xyz = "xyz";
sb = new StringBuilder(initialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.ToString());

推荐答案

你是说你想用你插入的那些
替换现有的角色?如果这就是你的意思,请尝试以下方法:


string initialValu =" " ;;

string xyz =" xyz";

sb = new StringBuilder(initialValue);

sb.Remove(3,xyz 。长度); //新代码

sb.Insert(3,xyz);

-

Kai Brinkmann [MSFT]


请不要直接向此别名发送电子邮件。此别名仅适用于

新闻组。

此帖子按原样提供。没有保证,也没有权利。

Matt <我******* @ Hotmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...
Are you saying you''d like to replace the existing character(s) with the ones
you''re inserting? If that''s what you mean try the following:

string initialValu = " ";
string xyz = "xyz";
sb = new StringBuilder(initialValue);
sb.Remove(3, xyz.Length); // new code
sb.Insert(3, xyz);
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"Matt" <me*******@Hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
10个空格指定的字符串会增长。
有没有办法阻止字符串的增长。它在
test.file上变成了14个空格。

我希望能够插入第3个位置但是长度应该保持10个

string initialValue =" " ;;
string xyz =" xyz";
sb = new StringBuilder(initialValue);
sb.Insert(3,xyz);
m.Writer(" c:/test.txt" ;, sb.ToString());
10 len space designated strings grows when i do sb.insert.
is there way to stop string growing. It becomes 14 space len on
test.file

I like to be able insert 3rd position but length should stay 10

string initialValue = " ";
string xyz = "xyz";
sb = new StringBuilder(initialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.ToString());



您可能必须使用StringBuilder.Remove来删除字符数,然后插入它们。


" Matt" <我******* @ Hotmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...
You might have to use StringBuilder.Remove to remove the number of character
that you want to replace, then insert them.

"Matt" <me*******@Hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
10个空格指定的字符串会增长。
有没有办法阻止字符串的增长。它在
test.file上变成了14个空格。

我希望能够插入第3个位置但是长度应该保持10个

string initialValue =" " ;;
string xyz =" xyz";
sb = new StringBuilder(initialValue);
sb.Insert(3,xyz);
m.Writer(" c:/test.txt" ;, sb.ToString());
10 len space designated strings grows when i do sb.insert.
is there way to stop string growing. It becomes 14 space len on
test.file

I like to be able insert 3rd position but length should stay 10

string initialValue = " ";
string xyz = "xyz";
sb = new StringBuilder(initialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.ToString());



这是非常迂回的。


执行插入更容易,然后将Length属性设置为

长度。你不会冒着过度复杂计算的风险。


基本上,你会这样做:


string initialValu =" " ;;

string xyz =" xyz";

sb = new StringBuilder(initialValue);

sb.Insert(3,xyz );

sb.Length = 10;


这样就可以了。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Kai Brinkmann [MSFT]" < KA ****** @ online.microsoft.com>写在消息

新闻:uJ ************** @ TK2MSFTNGP09.phx.gbl ...
That''s pretty roundabout.

It''s easier to perform the insert, and then set the Length property to
the length. You don''t run the risk of over complicating the calculation.

Basically, you would do this:

string initialValu = " ";
string xyz = "xyz";
sb = new StringBuilder(initialValue);
sb.Insert(3, xyz);
sb.Length = 10;

And that would do it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
你在说你吗?我想用你插入的那些替换现有的角色吗?如果这就是你的意思,请尝试以下方法:

string initialValu =" " ;;
string xyz =" xyz";
sb = new StringBuilder(initialValue);
sb.Remove(3,xyz.Length); //新代码
sb.Insert(3,xyz);
-
Kai Brinkmann [MSFT]

请不要直接向此发送电子邮件别名。此别名仅用于
新闻组。
此帖子按原样提供。没有保证,也没有赋予
权利。

" Matt" <我******* @ Hotmail.com>在消息中写道
新闻:11 ********************** @ g49g2000cwa.googlegr oups.com ...
Are you saying you''d like to replace the existing character(s) with the
ones you''re inserting? If that''s what you mean try the following:

string initialValu = " ";
string xyz = "xyz";
sb = new StringBuilder(initialValue);
sb.Remove(3, xyz.Length); // new code
sb.Insert(3, xyz);
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Matt" <me*******@Hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
当我执行sb.insert时,10个空格指定的字符串会增长。
是否有办法阻止字符串的增长。它在
test.file上变成了14个空格。

我希望能够插入第3个位置但是长度应该保持10个

string initialValue =" " ;;
string xyz =" xyz";
sb = new StringBuilder(initialValue);
sb.Insert(3,xyz);
m.Writer(" c:/test.txt" ;, sb.ToString());
10 len space designated strings grows when i do sb.insert.
is there way to stop string growing. It becomes 14 space len on
test.file

I like to be able insert 3rd position but length should stay 10

string initialValue = " ";
string xyz = "xyz";
sb = new StringBuilder(initialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.ToString());




这篇关于String Builder插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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