Home c# Line break after N characters in c #

Line break after N characters in c #

Author

Date

Category

I saw examples in various languages, but for c # I did not understand how to implement this, I ask for help. There is text, for example 20 characters. How to make a line break after 10 characters, for example? Thank you.


Answer 1, authority 100%

The easiest way to write code is to use regular expressions: http://ideone.com/rbR09w

public static string SplitToLines (string str, int n)
{
 return Regex.Replace (str, ". {" + n + "} (?! $)", "$ 0 \ n");
}

Answer 2, authority 60%

Another option:

string text = "abvgdeozhziyiklmn";
int k = 10, i = text.Length;
text = text.Substring (0, k) + "\ n" + text.Substring (k + 1, i-k-1);

By tym32167 :

text = text.Substring (0, k) + Environment.NewLine + text.Substring (k + 1, i - k - 1);

Answer 3, authority 40%

A good way of capturing performance is StringBuilder: http://ideone.com/Mmnvjp

public static string SplitToLines (string str, int n)
{
 var sb = new StringBuilder (str.Length + (str.Length + 9) / 10);
 for (int q = 0; q & lt; str.Length;)
 {
  sb.Append (str [q]);
  if (++ q% n == 0)
   sb.AppendLine ();
 }
 if (str.Length% n == 0)
  --sb.Length;
 return sb.ToString ();
}

Answer 4, authority 40%

This option …

using static System.Console;
class Program
{
  public static string Insert_LF_n (string s, int n)
  {
    char [] c = new char [s.Length + s.Length / n];
    int i = 0, j = 1;
    foreach (char ch in s)
    {
      c [i ++] = ch;
      if (j ++% n == 0)
      {
        c [i ++] = '\ n';
        j = 1;
      }
    }
    // if the last character is "line feed" - delete it.
    if (c [i - 1] == '\ n')
      i--;
    return new string (c, 0, i);
  }
  static void Main (string [] args)
  {
    string s = "kjabdjlirujl; qkmrghwiureh; alksngk; jhdriughjksndv.ma'pjkrdoigkdfjnvsdlkjfp'iowjoeijrlkdjflk";
    WriteLine (s);
    Write ('\ n' + Insert_LF_n (s, 10));
    ReadKey ();
  }
}

Answer 5, authority 20%

string myString = "My very long string ................ ....................... ";
string rn = Environment.NewLine;
int offset = 10;
StringBuilder sb = new StringBuilder (myString);
for (int i = 1; i & lt; = myString.Length / offset; i ++)
{
  sb.Insert (i * offset + (i - 1) * rn.Length, rn);
}
Console.WriteLine (sb.ToString ());

Made via StringBuilder , should work better with very long line lengths.


Answer 6

Somehow it happened

int countOnOneLine = 10;
string testStr = "123456789D123456789D1111";
string resultStr = "";
for (int i = 1; i & lt; = testStr.Length; i ++)
{
  if (i% countOnOneLine == 0)
    resultStr + = testStr [i - 1] + Environment.NewLine;
  else
    resultStr + = testStr [i - 1];
}
Console.WriteLine (resultStr);
Console.ReadLine ();

PS: something tells me that there is a more adequate solution …


Answer 7

We start the loop from the starting position and insert the required line each time. Move 10 + the size of the inserted row. Etc.

Usage:

var result = InsertEach (source, Environment.NewLine, 10);

Method:

public static string InsertEach (string source, string insertValue, int interval)
{
  var lengthOfNewLine = insertValue.Length;
  for (var i = interval; i & lt; source.Length; i + = interval + lengthOfNewLine)
  {
    source = source.Insert (i, insertValue);
  }
  return source;
}

Test !


Answer 8

My version of Regex)

static void Main (string [] args)
  {
    Console.WriteLine (Regex ());
    Console.WriteLine (Iteration ());
    Console.ReadKey ();
  }
  static string Regex ()
  {
    string pattern = @ "(. {0,10})";
    string input = "qwertyuiopqqwertyuiopqwertyuiopwertyuio";
    string replacement = "$ 1 \ n";
    Regex rgx = new Regex (pattern);
    string result = rgx.Replace (input, replacement);
    return result;
  }
  static string Iteration ()
  {
    int n = 10;
    string input = "qwertyuiopqqwertyuiopqwertyuiopwertyuio";
    for (int i = n; i & lt; input.Length; i + = n + 1)
    {
      input = input.Insert (i, "\ n");
    }
    return input;
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions