fork download
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. new Test1().Main();
  10. }
  11. }
  12.  
  13. public class Test1
  14. {
  15. void Show(string res) {
  16. var resWrapped = "\"" + res.Replace("\n", "⏎\n").Replace(" ", "·").Replace("\t", "↹") + "\"";
  17. Console.WriteLine(resWrapped);
  18. Console.WriteLine();
  19. }
  20.  
  21. public void Main()
  22. {
  23. var indentAmount = 4;
  24. Show(Indent("", indentAmount));
  25. Show(Indent(@"
  26. ", indentAmount));
  27. Show(Indent(@"line1
  28. line2
  29.  
  30. line4hasATrailingSpace (line3wasEmpty)
  31. line5beginsWithExtraIndenation
  32.  
  33.  
  34. line8isHere (line6wasEmpty and line7hadOnlySpaces)
  35. line9hasEndOfString", indentAmount));
  36. }
  37.  
  38. string IndentWhoIsRich(string textToIndent, int indentAmount = 1, char indentChar = ' ')
  39. {
  40. var indent = new string(indentChar, indentAmount);
  41. return indent + textToIndent.Replace("\n", "\n" + indent);
  42. }
  43.  
  44. string IndentStakx(string str, int count = 1, char indentChar = ' ')
  45. {
  46. var indented = new StringBuilder();
  47. var i = 0;
  48. while (i < str.Length)
  49. {
  50. indented.Append(indentChar, count);
  51. var j = str.IndexOf('\n', i + 1);
  52. if (j > i)
  53. {
  54. indented.Append(str, i, j - i + 1);
  55. i = j + 1;
  56. }
  57. else
  58. {
  59. break;
  60. }
  61. }
  62. indented.Append(str, i, str.Length - i);
  63. return indented.ToString();
  64. }
  65.  
  66. string IndentMD1(string textToIndent, int indentAmount = 1, char indentChar = ' ')
  67. {
  68. var splittedText = textToIndent.Split(new string[] {Environment.NewLine}, StringSplitOptions.None);
  69. var indent = new string(indentChar, indentAmount);
  70. var sb = new StringBuilder();
  71. foreach (var line in splittedText) {
  72. sb.Append(indent);
  73. sb.AppendLine(line);
  74. }
  75. return sb.ToString();
  76. }
  77.  
  78. Regex regexForReplace = new Regex(@"(\n)(?!([\r\n]|$))");
  79. Regex regexForFirst = new Regex(@"^([\r\n]|$)");
  80.  
  81. string Indent(string textToIndent, int indentAmount = 1, char indentChar = ' ')
  82. {
  83. var indent = new string(indentChar, indentAmount);
  84. string firstIndent = regexForFirst.IsMatch(textToIndent) ? "" : indent;
  85. return firstIndent + regexForReplace.Replace(textToIndent, @"$1" + indent);
  86. }
  87.  
  88. }
Success #stdin #stdout 0.03s 134592KB
stdin
Standard input is empty
stdout
""

"⏎
"

"····line1⏎
····line2⏎
⏎
····line4hasATrailingSpace·(line3wasEmpty)·⏎
······line5beginsWithExtraIndenation⏎
⏎
······⏎
····line8isHere·(line6wasEmpty·and·line7hadOnlySpaces)⏎
····line9hasEndOfString"