• 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

    • Apple CarPlay to let you stream iPhone videos to your car screen by Sagar Naresh Bhavsar In an official post for developers, Apple announced that CarPlay users will be able to watch iPhone videos on their car's display. This feature will be powered by Apple AirPlay, allowing users to stream videos wirelessly from their iPhones to their car's infotainment screen. For safety reasons, users will be able to enjoy videos on their car's display, but only when their vehicle is parked. Users won't be able to watch videos while the car is on the move. The connected iPhone will automatically detect when the car moves and will stop playing the video to avoid driver distraction. According to Apple, devs need to "integrate support for CarPlay with AirPlay video to enable this feature in your car." Carmakers will need to add support for CarPlay with AirPlay video to let users watch videos in their cars. So, this may take a while to roll out. Apple hasn't specifically mentioned whether it will be exclusive to the newly introduced and limited CarPlay Ultra, or standard CarPlay users will also be able to enjoy videos through AirPlay on their car's screen. Also, there is no clarity if existing models will get support via software update, or if it will be limited to newer models only. On the Android side, Google has also announced a bunch of new capabilities soon to be coming to Android Auto. The company has extended support for weather apps in beta for Android Auto and has confirmed that Android Auto will soon support video and browser apps as well. Image via Depositphotos
    • For a moment there I thought it speeds up Windows installation, but alas...
    • PNY's DUO LINK V3 promises truly incredible speed for a USB flash drive by Sayan Sen PNY today announced a new USB flash drive that blends modern design with high performance. The new DUO LINK™ V3 supports USB 3.2 Gen 2 and offers both Type-C and Type-A connectors. Housed in a metal shell with a matte black finish, the device is built for users who need reliable results when transferring and storing files. The drive is available in multiple storage sizes ranging from 256GB, all the way up to 2TB. According to PNY, the DUO LINK V3 is engineered to deliver read speeds of up to 1,000 MB/s and write speeds of up to 800 MB/s. These speeds are significantly higher than those achieved by standard USB 2.0 flash drives, allowing users to move large files. The product reminds us of the "Poxiao" flash memory we recently covered, which promises even more incredible speeds. The DUO LINK V3 flash drive’s design uses the common swivel mechanism with dual connectors. One side of the device features a USB Type-C connector, while the other side has a USB Type-A connector. This dual-connector system is intended to maximize compatibility, making the DUO LINK V3 useful across a broad range of devices, including smartphones, tablets, laptops, and desktop computers. Thus, in practical terms, users can easily manage files between newer mobile devices and traditional computers without needing extra adapters. Durability and ease of use are emphasized by the metal housing, which is designed to protect the drive during everyday handling. A built-in key loop further supports portability, an important factor for professionals who need to carry important data with them. The device also maintains backward compatibility with older USB standards such as USB 3.2 Gen 1, USB 3.0, and USB 2.0, ensuring that users will find it adaptable to many existing systems. The DUO LINK V3 flash drive is now available directly from PNY and through selected online marketplaces like Amazon. The pricing starts at $34.99 for the 256GB version and reaches $159.99 for the 2TB version. This release provides users with a tool that meets the growing demands of data-intensive work while remaining accessible to those needing a reliable and versatile storage solution. This article was generated with some help from AI and reviewed by an editor. As an Amazon Associate we earn from qualifying purchases.
    • Message from Campbell Wilson, MD & CEO, Air India: https://www.instagram.com/airindia/reel/DKzZQUPhafx/
    • I mean they have been proven to be safer than human drivers in many cases so at the end of the day they will be allowed on the road, the fact that you don't trust them doesn't mean that policy has to be changed to make you more comfortable I am afraid.
  • Recent Achievements

    • Week One Done
      fashionuae earned a badge
      Week One Done
    • One Month Later
      fashionuae earned a badge
      One Month Later
    • Week One Done
      elsafaacompany earned a badge
      Week One Done
    • Week One Done
      Yianis earned a badge
      Week One Done
    • Veteran
      Travesty went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      508
    2. 2
      ATLien_0
      263
    3. 3
      +FloatingFatMan
      193
    4. 4
      +Edouard
      174
    5. 5
      snowy owl
      125
  • Tell a friend

    Love Neowin? Tell a friend!