Appending a string

  • Thread starter Thread starter Paul Roberts
  • Start date Start date
P

Paul Roberts

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
 
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,
 
string x = "your long string";

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

Steve said:
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,
 
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.
 
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.
 
Hi,


Peter Rilling said:
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,
 
Can someone give me a head start? i'm having problems with it, and dont know
where to start.
 
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!
 
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;
}
 
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;
}
 
Bruce Wood said:
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);
 
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
 
Chris Springer said:
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.
 
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
 
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.
 
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 ??????
 
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);
 
Back
Top