title: How to keep the carriage return by Ignoring the carriage return C#
body: <p>I thank you in advance.</p>
  <p>I have a function that removes any number of characters past the maximum length. However, it also removes carriage returns. So, if I write a paragraph and use carriage returns for the new paragraph the carriage returns would also be counted and removed.</p>
  <p>Does anybody have a good idea how I can achieve removing anything over the maximum length of characters without removing or counting the carriage returns?</p>
  <pre><code>    public static string RemoveExcessCharacters(string value, int maxLen)
      {
          return (value.Length &gt; maxLen) ? value.Substring(0, maxLen) : value;
      }
  </code></pre>
  <p>The input code. Some of the code I was just testing like. newString in the code.</p>
  <pre><code>        private void txtWrite_TextChanged(object sender, EventArgs e)
      {
          try
          {
              int g = Convert.ToInt32(txtNumbersOnly.Text);
              if (string.IsNullOrEmpty(txtNumbersOnly.Text) || g == 0)
              {
                  txtWrite.Text = &quot;&quot;;
                  MessageBox.Show(&quot;Please provide character Count. Character Count cannot be zero or Null.&quot;, &quot;Maximum Characters&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information);
                  txtNumbersOnly.Text = &quot;777&quot;;
                  txtNumbersOnly.Focus();
                  return;
              }
              else
              {
                  cCount = Convert.ToInt32(txtNumbersOnly.Text);
                  if (cCount == 0)
                  {
                      txtWrite.Text = &quot;&quot;;
                      MessageBox.Show(&quot;Please provide character Count. Character Count cannot be zero.&quot;, &quot;Maximum Characters&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information);
                      txtNumbersOnly.Focus();
                      return;
                  }
                  else
                  {
                      // Testing again
                      //x = txtWrite.Text.TrimEnd();
                      x = txtWrite.Text;
                      //var newString = string.Join(&quot; &quot;, Regex.Split(x, @&quot;(?:\r\n|\n|\r)&quot;)).Length;
                      //var newString = string.Join(&quot; &quot;, Regex.Split(x, @&quot;(?:\r\n|\n|\r)&quot;)).Length;
                      //string v = x.Replace(&quot;\n&quot;, &quot;&quot;).Replace(&quot;\r&quot;, &quot;&quot;);
                      string cleaned = x;
                      string cleaned2 = cleaned.Length.ToString();
                      int cleaned3 = Convert.ToInt32(cleaned2);
                      lblChar.Text = &quot;Character Count: &quot; + cleaned2;
                      //
                      if (cleaned3 &gt; cCount)
                      {
                          string strTest = x.ToString();
                          MessageBox.Show(&quot;You are over the maximum character length.\nI will trim this down for you now.&quot;, &quot;Maximum Character Length&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information);
                          int intTrans = Convert.ToInt32(x.Length);
                          if (intTrans &gt; cCount)
                          {
                              // Remove ending Characters over the max amount.
                              string value = RemoveExcessCharacters(txtWrite.Text, cCount);
                              // Re-add corrected text to the textbox.
                              txtWrite.Text = value;
                              // Move cursor to end
                              txtWrite.SelectionStart = txtWrite.Text.Length;
                              return;
                          }
                      }
                      else
                      {
                          return;
                      }
                  }
              }
              //
              return;
              //
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
              return;
          }
      }
  </code></pre>