'How to keep the carriage return by Ignoring the carriage return C#
I thank you in advance.
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.
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?
public static string RemoveExcessCharacters(string value, int maxLen)
{
return (value.Length > maxLen) ? value.Substring(0, maxLen) : value;
}
The input code. Some of the code I was just testing like. newString in the 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 = "";
MessageBox.Show("Please provide character Count. Character Count cannot be zero or Null.", "Maximum Characters", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtNumbersOnly.Text = "777";
txtNumbersOnly.Focus();
return;
}
else
{
cCount = Convert.ToInt32(txtNumbersOnly.Text);
if (cCount == 0)
{
txtWrite.Text = "";
MessageBox.Show("Please provide character Count. Character Count cannot be zero.", "Maximum Characters", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtNumbersOnly.Focus();
return;
}
else
{
// Testing again
//x = txtWrite.Text.TrimEnd();
x = txtWrite.Text;
//var newString = string.Join(" ", Regex.Split(x, @"(?:\r\n|\n|\r)")).Length;
//var newString = string.Join(" ", Regex.Split(x, @"(?:\r\n|\n|\r)")).Length;
//string v = x.Replace("\n", "").Replace("\r", "");
string cleaned = x;
string cleaned2 = cleaned.Length.ToString();
int cleaned3 = Convert.ToInt32(cleaned2);
lblChar.Text = "Character Count: " + cleaned2;
//
if (cleaned3 > cCount)
{
string strTest = x.ToString();
MessageBox.Show("You are over the maximum character length.\nI will trim this down for you now.", "Maximum Character Length", MessageBoxButtons.OK, MessageBoxIcon.Information);
int intTrans = Convert.ToInt32(x.Length);
if (intTrans > 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;
}
}
Solution 1:[1]
From your comment on my other answer it seems you want a remove function that ignores certain characters when it is removing. "this is some text".RemoveIgnoring(6, " \r\n") produces "this is"
public static string RemoveIgnoring(this string original, int howMany, string ignore){
var sb = new StringBuilder();
for(; howMany > 0 && sb.Length < original.Length; howMany -= ignore.Contains(original[sb.Length-1])? 0:1)
sb.Append(original[sb.Length]);
return sb.ToString();
}
The way it works is to loop over the string accumulating characters into a StringBuilder but it only decrements howMany if the char does not appear in the ignore string. It means that eg spaces or new lines are "free" additions whereas everything else "costs 1", eventually all the "money" (the howMany variable) is used up
Solution 2:[2]
Don't cut the end off the user's words; stackoverflow doesn't when you write a comment - it just counts down the chars you have left and let's you exceed but doesn't submit. This is nicer because it's not arbitrarily deciding to delete what the user typed
As such, a simple button click submit validation of:
public void SomeButton_Click(object sender, EventArgs e){
if(someTextBox.Text.Count(c => !"\r\n".Contains(c)) > 500)
MessageBox.Show("You have exceeded 500 chars, not including new lines. Shorten your message before you submit");
else
SubmitMessage(someTextBox.Text);
}
You can apply the same technique to the TextChanged event if you want to eg provide a label that constantly counts down as the user types
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Caius Jard |
