473,797 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Appending a string

Hiya

I have a string with about 300+ characters, how can i insert a line break or
another character every 50 characters for the whole string?

Paul Roberts
Dec 9 '05
18 2915
I'm using this code, which works if the string is in multiples of 2, but if
not a get an error of OutOfRange, how can i fix this?
int cursor = 0;
string hello = "1234567890 0";

StringBuilder bs = new StringBuilder() ;

while (cursor < hello.Length)
{
bs.Append (hello.Substrin g(cursor, 2));
bs.Append ("HERE");
cursor += 2;
}
Dec 9 '05 #11
Change your while loop to:

while (cursor < hello.Length)
{
int snippetLength = Math.Min(2, hello.Length - cursor);
bs.Append (hello.Substrin g(cursor, snippetLength)) ;
bs.Append ("HERE");
cursor += snippetLength;
}

Dec 9 '05 #12
Bruce Wood <br*******@cana da.com> wrote:
Change your while loop to:

while (cursor < hello.Length)
{
int snippetLength = Math.Min(2, hello.Length - cursor);
bs.Append (hello.Substrin g(cursor, snippetLength)) ;
bs.Append ("HERE");
cursor += snippetLength;
}


Note that that still involves a lot of unnecessary copying - all those
Substring calls.

Instead, use bs.Append (hello, cursor, snippetLength);

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 9 '05 #13
Did everyone forget that were only talking about 300+ characters. Use the
short method...don't overkill it.

Chris

--
Securing your systems is much like fighting off disease -- as long as you
maintain basic hygiene, you're likely to be okay, but you'll never be
invulnerable.

Steve Shah - Unix Systems Network Administrator
"Steve" <st**********@t icketmaster.com > wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
string x = "your long string";

for (int i = 50; i < x.Length; i += 50)
x = x.Insert(i, System.Enviornm ent.NewLine);

Dec 10 '05 #14
Chris Springer <cs*****@adelph ia.net> wrote:
Did everyone forget that were only talking about 300+ characters. Use the
short method...don't overkill it.


It depends whether "300+" means "over 300" or "just a bit over 300". If
it's the former, it could be huge. I agree it's generally best to go
with the simple solution - worth bearing the more efficient one in mind
in this case though, possibly making a note in a comment in case the
method ends up being a bottleneck.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 10 '05 #15
Yup... String.Concat is equal to StringBuilder up to 7 strings of 25
characters
length. At 13 strings, StringBuilder is twice as efficient as
String.Concat.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Dec 10 '05 #16
Just a quick post to say thanks for all your help, i've managed to get it to
work the way i intended it too.
Dec 11 '05 #17
On Sun, 11 Dec 2005 02:36:42 +0000, Paul Roberts wrote:
Just a quick post to say thanks for all your help, i've managed to get it to
work the way i intended it too.


And after so many good suggestiosn and assistance, you could post back to
the newsgroup what your solution was ??????
Dec 11 '05 #18
I'll be glad too, and hopfully it may help someone else out to :)

int cursor = 0;
StringBuilder sb = new StringBuilder() ;

while (cursor < longstring.Leng th)
{
int size = Math.Min (50, longstring.Leng th - cursor);
sb.Append (longstring, cursor, size);
sb.Append ("\r\n");
cursor += size;
}

Console.WriteLi ne(sb);

"Jerry Walter" <gw******@woh.r r.com> wrote in message
news:pa******** *************** *****@woh.rr.co m...
On Sun, 11 Dec 2005 02:36:42 +0000, Paul Roberts wrote:
Just a quick post to say thanks for all your help, i've managed to get it
to
work the way i intended it too.


And after so many good suggestiosn and assistance, you could post back to
the newsgroup what your solution was ??????

Dec 12 '05 #19

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

Similar topics

2
1610
by: Robizzle | last post by:
I am having problems appending a number to the end of a string. I searched google and google forums and couldn't figure it out .... sorry i'm sure its simple. so I have: $filename = 'test.file."; $i = 0; and I want to change $filename to become 'test.file.0'.
16
12644
by: Michael | last post by:
I have a data application in a2k that I need to create two fixed width text files and then combine them to a single file The first file is header information and the second is transaction data. I have tried and tried but just cant seem to get this right, I am using Queries to created my export files with specifications which works fine, I get stumped with the appending the header to my transaction file. What I have so far looks like...
3
1933
by: MLH | last post by:
I have a query, qryAppend30DayOld260ies that attempts to append records to tblCorrespondence. When run, it can result in any of the following: appending no records, appending 1 record or appending many records. Two of the target fields in tblCorrespondence receiving values in the append operation are and . For any given VehicleJobID value, I want only ONE record in correspondence table to have an value of "01". This query blindly appends...
1
2716
by: mikemac76 | last post by:
I am trying to build a test harness to practice appending strings to an rtf string. I am doing this to simulate the string I'll be sending over the wire and receiving on the other end of an IM application. Below is the code I use to test. I receive this error everytime I try to send the string from one rtb to another: "File Format is not valid Line 28" Line 28 is where I set rtb2 to the value of the newly concatanated...
2
2243
by: tony.collings | last post by:
I started a thread here : http://groups.google.co.uk/group/microsoft.public.cmserver.general/browse_thread/thread/29d63077144004a9/c3888efdcb7338f6?hl=en#c3888efdcb7338f6 About creating an Email function to email authors when the Review Date has expired on their page. I've managed to now achieve a significant proportion of the work by writing out the details to an XML file and reading them back. However I'm a little stuck on Amending...
4
1671
by: John A Grandy | last post by:
could someone explain the following to me : Appending the literal type character I to a literal forces it to the Integer data type. Appending the identifier type character % to any identifier forces it to Integer.
3
565
by: Jim | last post by:
Could anyone please point me towards some code that allows me to add to an existing XML file using the output of an HTML form. I want to add a form on my website so users can input their email address in a text field and feedback in a text area and then submit it. I can output the data no probs but appending it first to the XML file is proving tricky. Any ideas?
1
1565
by: libsfan01 | last post by:
hello again! another problem im having is appending a value to a string. how come this function isnt working for me? string overwrites the value of content not gets added onto the end of it?! function alertchange(value) { var string = value;
7
9509
by: Daz | last post by:
Hi everyone! Is it possible to take a line of text like so: <tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr> And append it to a DOM node such as this: var nodeToAppendTo = document.getElementById('tbody');
1
3884
by: KiddoGuy | last post by:
I am trying to build a string character by character. However, when I use the overloaded += or string.append() method, the character replaces the one before it rather than appending to it so I am constantly left with a string of length 1. Here's a sample of what I'm trying to do. char cur; while(!fin.eof()) { //(fin is an ofstream) string currstr = ""; //This is part of what's giving me a problem /* build strings from...
0
9685
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
10470
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
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
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
7561
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
6803
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
5459
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.