Re: String Builder insert
- From: "Kai Brinkmann [MSFT]" <kaibrink@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 15 Nov 2005 13:06:28 -0800
Well, that works for this specific example of course. But by simply
resetting sb.Length after inserting you are truncating as many characters as
you just inserted from the end of the StringBuilder instance. That's fine if
we're just dealing with spaces (as in this example), but it might not be
what you want in all cases.
--
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.
"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:eorfjbi6FHA.2600@xxxxxxxxxxxxxxxxxxxxxxx
> 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]
> - mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> "Kai Brinkmann [MSFT]" <kaibrink@xxxxxxxxxxxxxxxxxxxx> wrote in message
> news:uJS10Yi6FHA.3136@xxxxxxxxxxxxxxxxxxxxxxx
>> 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" <metehanIT@xxxxxxxxxxx> wrote in message
>> news:1132087015.064788.232780@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>>> 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());
>>>
>>
>>
>
>
.
- References:
- String Builder insert
- From: Matt
- Re: String Builder insert
- From: Kai Brinkmann [MSFT]
- Re: String Builder insert
- From: Nicholas Paldino [.NET/C# MVP]
- String Builder insert
- Prev by Date: Re: HOWTO Implement LoadLibrary, GetProcAdress, and FreeLibrary.
- Next by Date: Re: Checking for Other Apps running
- Previous by thread: Re: String Builder insert
- Next by thread: Re: String Builder insert
- Index(es):