Split string with maximum character limit

I am trying to split a string into many lines (list), each of which has a maximum character limit. Say, if I had a line of 500 characters, and I want each line to have a maximum of 75, there would be 7 lines, and the last would not have a full 75.

I tried some of the examples that I found on stackoverflow, but they "crop" the results. Any ideas?

+3
source share
6 answers

You can write your own extension method to do something like this

static class StringExtensions
{
    public static IEnumerable<string> SplitOnLength(this string input, int length)
    {
        int index = 0;
        while (index < input.Length)
        {
            if (index + length < input.Length)
                yield return input.Substring(index, length);
            else
                yield return input.Substring(index);

            index += length;
        }
    }
}

And then you can call it like this

string temp = new string('@', 500);

string[] array = temp.SplitOnLength(75).ToArray();

foreach (string x in array)
    Console.WriteLine(x);
+5
source

I would tackle this loop using the C # String.Substring method.

, , .

var myString = "hello world";
List<string> list = new List();
int maxSize
while(index < myString.Length())
{
  if(index + maxSize > myString.Length())
  {
    // handle last case
    list.Add(myString.Substring(index));
    break;
  }
  else
  {
    list.Add(myString.Substring(index,maxSize));
    index+= maxSize;
   }
}
+1

"split", split? , - :

List<string> list = new List<string>();
string s = "";
int num = 75;
while (s.Length > 0)
{
    list.Add(s.Substring(0, num));
    s = s.Remove(0, num);
}
+1
source

I think this is a little cleaner than the other answers:

    public static IEnumerable<string> SplitByLength(string s, int length)
    {
        while (s.Length > length)
        {
            yield return s.Substring(0, length);
            s = s.Substring(length);
        }

        if (s.Length > 0) yield return s;            
    }
+1
source

I suppose it could be a space character, like a space.

find the line (instr) until you find the next delimiter position.

if it is & lt; your substring length (75) is then added to the current substring.

if not, start a new substring.

special case - if there is no separator in the whole substring, then you need to determine what is happening - for example, add “-” and then continue.

0
source
    public static string SplitByLength(string s, int length)
    {
        ArrayList sArrReturn = new ArrayList();
        String[] sArr = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string sconcat in sArr)
        {
            if (((String.Join(" ", sArrReturn.ToArray()).Length + sconcat.Length)+1) < length)
                sArrReturn.Add(sconcat);
            else
                break;
        }
        return String.Join(" ", sArrReturn.ToArray());
    }

    public static string SplitByLengthOld(string s, int length)
    {
        try
        {
            string sret = string.Empty;
            String[] sArr = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string sconcat in sArr)
            {
                if ((sret.Length + sconcat.Length + 1) < length)
                    sret = string.Format("{0}{1}{2}", sret, string.IsNullOrEmpty(sret) ? string.Empty : " ", sconcat);
            }
            return sret;
        }
        catch
        {
            return string.Empty;
        }
    }
0
source

Source: https://habr.com/ru/post/1750941/


All Articles