473,657 Members | 2,597 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(i nitialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.To String());

Nov 17 '05 #1
13 5581
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(i nitialValue);
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*******@Hotm ail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(i nitialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.To String());

Nov 17 '05 #2
You might have to use StringBuilder.R emove to remove the number of character
that you want to replace, then insert them.

"Matt" <me*******@Hotm ail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(i nitialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.To String());

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(i nitialValue);
sb.Insert(3, xyz);
sb.Length = 10;

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

"Kai Brinkmann [MSFT]" <ka******@onlin e.microsoft.com > wrote in message
news:uJ******** ******@TK2MSFTN GP09.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(i nitialValue);
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*******@Hotm ail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(i nitialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.To String());


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(i nitialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.To String());

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

"Peter Rilling" <pe***@nospam.r illing.net> wrote in message
news:Ot******** ******@tk2msftn gp13.phx.gbl...
You might have to use StringBuilder.R emove to remove the number of
character that you want to replace, then insert them.

"Matt" <me*******@Hotm ail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(i nitialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.To String());


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.c om> wrote in
message news:eo******** ******@tk2msftn gp13.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(i nitialValue);
sb.Insert(3, xyz);
sb.Length = 10;

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

"Kai Brinkmann [MSFT]" <ka******@onlin e.microsoft.com > wrote in message
news:uJ******** ******@TK2MSFTN GP09.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(i nitialValue);
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*******@Hotm ail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(i nitialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.To String());



Nov 17 '05 #8

"Mihir Solanki" <mi**********@h otmail.com> wrote in message
news:19******** *************** **@msnews.micro soft.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.Len gth;
if (sb == null)
{
throw new ArgumentNullExc eption("sb", "Argument cannot be null");
}

if (iLen+startPosi tion > sb.Length)
{
throw new
ArgumentOutOfRa ngeException("s b",sb,string.Fo rmat("StringBui lder is not
large enough to support the replacement text '{0}' at the specified starting
position '{1}'.",replace With,startPosit ion));
}

for (int count = 0; count != iLen; count++)
{
sb[count+startPosi tion] = 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(i nitialValue);
sb.Insert(3, xyz);
m.Writer("c:/test.txt",sb.To String());

Nov 17 '05 #10

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

Similar topics

2
2129
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
2441
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
2
2350
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 single message at a time. For this I was using a string builder and appending all the text to it after each validation. This process slowed down the system and at a point I got the out of memory exception. Would a string builder consitsiting of 20-30...
11
8452
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 sb->Append('\r'); and also
3
3404
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 confusing. Also, I would like a good source on the naming conventions to be used for other objects. It seems there are tons of sights with naming conventions but none seem to contain a complete list for all the objects in .Net. Is there such a...
8
7680
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>" + string1 + "</td><td nowrap>" + formatR(string2, 2) + "</tD><td nowrap>" + string3 + "</td><td>") return webLine.ToString(); }
3
1932
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 StringBuilder(10000);
2
1657
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 file, i have used the following coding StreamReader reader = new StreamReader(@"d:\web_extract.txt",Encoding.Default); StringBuilder strbuild = null; StreamWriter writer = null; while(reader.Peek() >= 0) { string str = null;
6
2048
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: htmlEmail.Append("</div><div class=UnitPrice>"); htmlEmail.Append("Unit Price:"); htmlEmail.Append("</div></div><hr />"); if (ToCompanyName == "Company1") getLineItems_Company1(htmlFile,...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7321
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.