473,395 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 #1
18 2880
Hi,

Basically, you cannot, String is an inmutable class.
What you can do is split the string in pieces, append the piece to a
StringBuilder, append Environment.NewLine and repite the process.

I will let the coding to you, it's the fun part :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Paul Roberts" <Pa**********@compserve.com> wrote in message
news:dn*******************@news.demon.co.uk...
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 #2
string x = "your long string";

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

Dec 9 '05 #3
Hi,

"Steve" <st**********@ticketmaster.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
string x = "your long string";

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


It's better to use StringBuilder, with the above code you are generating a
lot of temp strings that consume resources.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 9 '05 #4
This is not very efficient since a new String object is created in each
loop.

"Steve" <st**********@ticketmaster.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
string x = "your long string";

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

Dec 9 '05 #5
Rather than splitting the string, he could just initialize a new
StringBuilder with that string and use StringBuilder.Insert to add the
characters. I think this will push the string.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:e$**************@tk2msftngp13.phx.gbl...
Hi,

Basically, you cannot, String is an inmutable class.
What you can do is split the string in pieces, append the piece to a
StringBuilder, append Environment.NewLine and repite the process.

I will let the coding to you, it's the fun part :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Paul Roberts" <Pa**********@compserve.com> wrote in message
news:dn*******************@news.demon.co.uk...
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 #6
If the string is, in fact, very long (thousands of characters) this
will result in a lot of unnecessary copying.

You're better off breaking up the string first, then gluing it back
together with newlines in between using StringBuilder.

Dec 9 '05 #7
Hi,
"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:ex****************@TK2MSFTNGP14.phx.gbl...
Rather than splitting the string, he could just initialize a new
StringBuilder with that string and use StringBuilder.Insert to add the
characters. I think this will push the string.


I think the same but I have no idea if that can be more efficient that
using Append , note that there is a version of Append that receive a string
and two integers and append that substring:
Append( string, start_index, end_index )

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 9 '05 #8
Can someone give me a head start? i'm having problems with it, and dont know
where to start.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:e$**************@tk2msftngp13.phx.gbl...
Hi,

Basically, you cannot, String is an inmutable class.
What you can do is split the string in pieces, append the piece to a
StringBuilder, append Environment.NewLine and repite the process.

I will let the coding to you, it's the fun part :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Paul Roberts" <Pa**********@compserve.com> wrote in message
news:dn*******************@news.demon.co.uk...
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 #9
Here are some hints.

Look at the documentation for the StringBuilder class. Notice that you
can create an empty StringBuilder and append things to it, then convert
the finished product to a string using the ToString() method of your
StringBuilder object.

If you really want every exactly 50 characters, just check into the
Substring method of String. If you need to do a more sophisticated
determination of where to break the string, write a method that takes a
string and an index and returns the length of the substring to break
off. For example, if you wanted to break the string at a blank space,
but at most 50 characters, a method like this might be useful:

private static int LineLength(string line, int startIndex)
{
int endIndex = startIndex + 50;
if (endIndex >= line.Length)
{
endIndex = line.Length - 1;
}
while (endIndex > startIndex && !Char.IsWhiteSpace(line[endIndex]))
{
endIndex--;
}
if (endIndex <= startIndex)
{
// return 50 or the remainder of the string, whichever is less
return Math.Min(50, line.Length - startIndex);
}
else
{
return endIndex - startIndex;
}
}

The part where you break up the string and reassemble it with newlines
would look like this:

int startIndex = 0;
StringBuilder sb = new StringBuilder();
while (startIndex < line.Length)
{
if (sb.Length > 0)
{
... append a newline to the StringBuilder ...
}
... append a chunk of the string "line" to the StringBuilder
... update "startIndex" to be the next place in the string "line"
to start
}
.... convert the StringBuilder to a string

and you're done!

Dec 9 '05 #10
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 = "12345678900";

StringBuilder bs = new StringBuilder();

while (cursor < hello.Length)
{
bs.Append (hello.Substring(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.Substring(cursor, snippetLength));
bs.Append ("HERE");
cursor += snippetLength;
}

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

while (cursor < hello.Length)
{
int snippetLength = Math.Min(2, hello.Length - cursor);
bs.Append (hello.Substring(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.com>
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**********@ticketmaster.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
string x = "your long string";

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

Dec 10 '05 #14
Chris Springer <cs*****@adelphia.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.com>
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.Length)
{
int size = Math.Min (50, longstring.Length - cursor);
sb.Append (longstring, cursor, size);
sb.Append ("\r\n");
cursor += size;
}

Console.WriteLine(sb);

"Jerry Walter" <gw******@woh.rr.com> wrote in message
news:pa****************************@woh.rr.com...
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
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 =...
16
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. ...
3
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...
1
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...
2
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...
4
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...
3
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...
1
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?! ...
7
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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...

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.