473,503 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Builder insert

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());

Nov 17 '05 #1
13 5570
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 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());

Nov 17 '05 #2
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 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());

Nov 17 '05 #3
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...
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 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());


Nov 17 '05 #4
Thanks all

Nov 17 '05 #5
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
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());

Nov 17 '05 #6
Or, just set the Length property to the length desired....
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:Ot**************@tk2msftngp13.phx.gbl...
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 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());


Nov 17 '05 #7
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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eo**************@tk2msftngp13.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...
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 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());



Nov 17 '05 #8

"Mihir Solanki" <mi**********@hotmail.com> wrote in message
news:19*************************@msnews.microsoft. com...
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
Nov 17 '05 #9
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]

Matt wrote:
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());

Nov 17 '05 #10
There's also StringBuilder.Replace

Nov 17 '05 #11
Reginald Blue wrote:
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];
}

--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 17 '05 #12
Matt <me*******@Hotmail.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #13
I am listening.
Never thought that way. actualy in a way replace would work too.

Nov 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2117
by: Karuppasamy | last post by:
Hi I am using a variable of StringBuilder to form a string. After appending a string of value like this {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Verdana;}}
12
2427
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
2
2342
by: P K | last post by:
Hi, I have around 30 fields on a screen each one of which I need to validate. The idea was to have a set of messages to be displayed for all the posiible errors instead of having to show a...
11
8431
by: TheRain | last post by:
Hi, I am trying to append a carriage return to my string using the string builder class, but when I do this the string ends up containing "13". I tried this multiple ways like so ...
3
3379
by: rsine | last post by:
I have searched around a little and have yet to find a naming convention for the string builder object. I really do not want to use "str" since this is for string objects and thus could be...
8
7664
by: simonZ | last post by:
I have Function with string parameteres: public string newLine(String string1,String string2,String string3){ StringBuilder webLine = new StringBuilder(); webLine.Append("<tD nowrap>" +...
3
1920
by: simonZ | last post by:
I read some articles about stringBuilder vs string. I'm using the loop, so I must use string builder. My example: String s1; String s2; String s3; StringBuilder webLines=new...
2
1647
by: rocksoft | last post by:
Hi, I have used stream reader to read text file using Readline method, and i tried to append all string to string builder, but i'm not able to get all string value, i'm only getting last line of...
6
2041
by: mcfly1204 | last post by:
I have a method where I am building an html email. Part of the way through the method, I have an if-statement that direct to another method based off of a field. For example: ...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.