• 0

C# Split a string every xxx characters


Question

It's pretty simple. I have a string of an unknown length, that needs to be displayed. Unfortunately, in Windows Phone, if your string of text displayed is over 2000-some pixels in height or width, it just sort of stops displaying. I've managed go display an unknown number of text strings, but I don't know how to split the text string up into an unknown, non-character delineated, string.

I can think of some pretty complex things, but isn't there something simple like this?

if (fullString.length > 1000)
{
array[] = fullString.Split(string.length(1000));

foreach (string chunk in array)
    {
    print(chunk);
    }
}
else
{
    print(fullString);
}

9 answers to this question

Recommended Posts

  • 0

You could simply do:

string content = new string(fullString.Take(1000));

Take is a LINQ method that works on an IEnumerable<char> (which a string is). The string(char[]) is used to construct a new string which you can then print.

  • 0

Not really like that. You can use the substring method to get a portion of it.

int chunkSize = 1000;
        int stringLength = str.Length;
        for (int i = 0; i &lt; stringLength ; i += chunkSize)
        {
            if (i + chunkSize &gt; stringLength) chunkSize = stringLength  - i;
            print(str.Substring(i, chunkSize));

        }

  • 0

If it were me, I'd make an extension like this:

        public static IEnumerable&lt;string&gt; SplitIntoLengths(this string input, int characterCount)
        {
            int index = 0;
            return from c in input
                   let itemIndex = index++
                   group c by itemIndex / characterCount
                   into g
                   select new string(g.ToArray());
        }

  • 0

You can also do it this way if it's more efficient for you >.<

String text;
int i = 0;
String[] array = new String[3];

while(i &lt; text.Length())
{
i++;
array[i % 3] += text[i];
}

foreach (string chunk in array)
{
print(chunk);
}

  • 0

I ran into this issue too. Quite frustrating, but I can understand the reasoning behind it. I was trying to display XHTML, so my solution was to split the string up into separate paragraphs and linebreaks, and put each paragraph in its own TextBlock. That worked beautifully.

  • 0
  On 16/08/2011 at 04:49, articuno1au said:

You can also do it this way if it's more efficient for you >.<

String text;
int i = 0;
String[] array = new String[3];

while(i &lt; text.Length())
{
i++;
array[i % 3] += text[i];
}

foreach (string chunk in array)
{
print(chunk);
}

That won't work, at least if you want your text coming out anything like the original. If printed without a newline, it would look like gibberish, or with it would put your text vertically wrapped at 3 characters. Also you'd get an index out of bounds since you're incrementing before using.

  • 0
  On 16/08/2011 at 04:58, AstareGod said:

I ran into this issue too. Quite frustrating, but I can understand the reasoning behind it. I was trying to display XHTML, so my solution was to split the string up into separate paragraphs and linebreaks, and put each paragraph in its own TextBlock. That worked beautifully.

That's what I had to do, too. My problem came when my paragraphs were still too big :blink:

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.