Split string after certain character count

前端 未结 4 1270
刺人心
刺人心 2020-12-11 07:25

I need some help. I\'m writing an error log using text file with exception details. With that I want my stack trace details to be written like the below and not in straight

4条回答
  •  感动是毒
    2020-12-11 08:09

    Best , Easiest and Generic Answer :). Just set the value of splitAt to the that number of character count after that u want it to break.

    string originalString = "1111222233334444";
    List test = new List();
    int splitAt = 4; // change 4 with the size of strings you want.
    for (int i = 0; i < originalString.Length; i = i + splitAt)
    {
        if (originalString.Length - i >= splitAt)
            test.Add(originalString.Substring(i, splitAt));
        else
            test.Add(originalString.Substring(i,((originalString.Length - i))));
    }
    

提交回复
热议问题