471,338 Members | 1,030 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 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 2786
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Robizzle | last post: by
1 post views Thread by mikemac76 | last post: by
2 posts views Thread by tony.collings | last post: by
4 posts views Thread by John A Grandy | last post: by
3 posts views Thread by Jim | last post: by
1 post views Thread by libsfan01 | last post: by
reply views Thread by rosydwin | last post: by

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.