• 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.
  • Posts

    • “Ask Photos” is coming to more Google Photos users in the US by Paul Hill Google has announced several improvements to a Gemini-powered Google Photos feature called Ask Photos. The feature launched last year in early access and it was great for longer search requests, however, when users typed “Dog” or “Beach” looking for those types of pictures, Ask Photos underperformed. Now, Google has addressed this and is bringing the feature to more eligible users in the United States. The search giant said that users enjoyed asking queries such as “suggest photos that’d make great phone backgrounds” or “what did I eat on my trip to Barcelona?” and getting Gemini-powered responses to these complex questions. Unfortunately, simple searches like “beach” or “dogs” were generating less than optimal results and people complained. To remedy the situation, Google has brought the best of Google Photos’ classic search feature to Ask Photos to improve latency. Easy, short requests will be dealt with by the old search mechanism and when you ask complex queries, it will switch over to the Gemini-powered search results. Now that Google has addressed this main issue of simple searches by integrated functionality from the previous search model, the company feels more confident to open up beyond early access and deliver it to more users in the United States. To be eligible, you must be 18+, be in the US, have your Google Account language set to English (United States), and have Face Groups turned on. This feature is only available on Android and iOS. To start using it, open Google Photos and tap on Ask at the bottom, then press Try now and agree to the terms. If you do not see the Ask button, it means you’re not yet eligible. The feature also can’t be used on computers yet. Hopefully, Google will quickly expand this feature outside the US so that international users can try it out too.
    • When are MS going to learn. Traditional home users who email, watch cat videos and perform basic tasks don't care if W11 is slightly faster...assuming it's true. Few home users care about faster speed and more secure claims.
    • They have. https://www.xda-developers.com...-25h2-update-kind-of-small/
    • Microsoft shares detailed performance benchmarks for Windows 11 vs 10 to show the faster PC by Sayan Sen In the past, Microsoft has always made big claims regarding Windows 11 performance and how it is faster and better than Windows 10. For example, back in 2021, the company stated how the former was designed to get the best out of your system hardware. Later in 2023, Microsoft shared detailed Windows 11 performance improvements and upgrades it achieved on the new OS. While the claims were not substantiated with data, they were certainly quite interesting. Then, in 2024, Microsoft highlighted the differences in performance, citing a paid study, to show Windows 11 was way faster. As with most commissioned studies, the data was not represented fairly. This week, Microsoft has shared new numbers based on its December 2024 testing in a new support document. Here are the performance claims Microsoft made: Top Windows 11 PCs have up to 2.7 more hours of battery life than Windows 10 PCs Windows 11 PCs are up to 2.3x faster than Windows 10 PCs Windows 11 PCs offer up to 3.2x faster web browsing than Windows 10 PCs Windows 11 PCs offer up to 2x faster Microsoft Office productivity than Windows 10 PCs Thus, if you ask Microsoft, it will say that Windows 11 is better than Windows 10 in everything there is, be it web browsing, Office use, battery life, and overall performance in general. The company notes that the results are "based on a 95% confidence interval" for each OS across multiple tests. Microsoft also accounted for outliers. To reach such numbers, Microsoft used the following test metrics: For battery life, a local 1080p 24 fps MP4 video file playback was tested till 90% battery discharge on the Windows Media Player app. During the test, all settings were default except that screen brightness was set to 150 nits and Auto-brightness was disabled. Wi-Fi was connected to a network. For the "2.3x faster" claim, Geekbench 6 results have been cited. For web browsing, Speedometer was used, though Microsoft does not mention the version. And finally, for Office, Procyon Office productivity was used. Interestingly, Microsoft only used Intel CPUs (the company also recently recommended Intel chips for Windows 11 Pro PCs). The company has played it a smarter this time as it has "tested performance, battery life and application capabilities of a selection of Windows 11 PCs in comparison to a selection of Windows 10 PCs", where the Windows 10 PCs feature Intel Core 6th, 8th, and 10th gen chips and Windows 11 PCs pack Intel Core 12th and 13th gen. Thus, this is Microsoft essentially acknowledging that the underlying hardware itself actually plays a big role in the claims it has made. But to be fair to the company, there is also no way to run Windows 11, at least officially, on unsupported PCs, such that a direct comparison can be made. You can find the support document with full battery results in this article here on Microsoft's website.
    • So, do nothing basically, as long as you deploy Windows Updates? Pretty much yeah. Some Linux distros distribute update the secureboot certs as well, assuming you do use SecureBoot.
  • Recent Achievements

    • Week One Done
      Marites earned a badge
      Week One Done
    • One Year In
      runge100 earned a badge
      One Year In
    • One Month Later
      runge100 earned a badge
      One Month Later
    • One Month Later
      jfam earned a badge
      One Month Later
    • First Post
      TheRingmaster earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      567
    2. 2
      +FloatingFatMan
      177
    3. 3
      ATLien_0
      169
    4. 4
      Michael Scrip
      127
    5. 5
      Xenon
      119
  • Tell a friend

    Love Neowin? Tell a friend!