String Builder insert

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

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());
 
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.
 
You might have to use StringBuilder.Remove to remove the number of character
that you want to replace, then insert them.
 
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]
- (e-mail address removed)

Kai Brinkmann said:
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 said:
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());
 
Not sure what you mean but you can do something like this if you just want
to make your string of fix size
lets say you got string with value "test.txt" and you want to make it of
size 10 with padded spaces on left.
you do something like this without using stringbuilder

string data = "test.txt";
data = data.PadLeft(" ",10);

it will make output string of exact 10 characters - " test.txt"

Mihir Solanki
http://www.mihirsolanki.com
 
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 said:
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]
- (e-mail address removed)

Kai Brinkmann said:
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 said:
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());
 
Mihir Solanki said:
Not sure what you mean but you can do something like this if you just want to make your string of
fix size
lets say you got string with value "test.txt" and you want to make it of size 10 with padded
spaces on left.
you do something like this without using stringbuilder

string data = "test.txt";
data = data.PadLeft(" ",10);

it will make output string of exact 10 characters - " test.txt"

Unless of course the original string was longer than 10 characters long.
In that case PadLeft/PadRight does nothing.

Bill
 
In addition to the other answers, I would like to point out that if the
initial value ever becomes significantly longer (say, 1k) you'll need to
switch to yet another algorithm. The problem with any approach that does
remove/insert is that it's going to be O(n) on the number of characters in
the string (as it has to move the characters around.)

I would encapsulate it in a function as such:

static void ReplaceAt(int startPosition, StringBuilder sb, string
replaceWith)
{
int iLen = replaceWith.Length;
if (sb == null)
{
throw new ArgumentNullException("sb", "Argument cannot be null");
}

if (iLen+startPosition > sb.Length)
{
throw new
ArgumentOutOfRangeException("sb",sb,string.Format("StringBuilder is not
large enough to support the replacement text '{0}' at the specified starting
position '{1}'.",replaceWith,startPosition));
}

for (int count = 0; count != iLen; count++)
{
sb[count+startPosition] = replaceWith[count];
}
}

I think that should do the trick.

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
 
Reginald said:
for (int count = 0; count != iLen; count++)
{
sb[count+startPosition] = replaceWith[count];
}
}

I think that should do the trick.

Close....
for (int count = 0; count != iLen; count++)
{
sb.Chars[count+startPosition] = replaceWith[count];
}
 
Matt said:
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

That's not inserting then. Insertion by definition increases the size
of a string. Are you actually looking to *replace* positions 3-5?
 
Back
Top