0

I am creating a project where the program fetches data from a serial port and displays it in a textbox. But along with the data, some unwanted characters are also getting inputted in the textbox through the serial port. I tried to trim them with '.Trim()' method by adding it in the textchanged event. But I could not get the desired outcome. I want the unwanted letters to get continuously trimmed from the textbox and the method should keep running in the background. Below is the code I tried out:

private void textBoxResults_TextChanged(...)
{
  char[] trim= ['a', 'b'];
  textBoxResults.Text.Trim(trim);
}

Please note that those are multiple letters(e.g. they could be g, m, a, etc.) I want all the unwanted ones to be trimmed from the textbox either directly when serialport data is received or after input from serial port.

D J
  • 845
  • 1
  • 13
  • 27
  • 1
    Example data and expected results would help your post. – Ryan Wilson May 08 '19 at 14:49
  • `Trim()` doesn’t remove the characters from the string. It removes them from the beginning and the end. It would be better to filter the input before adding it to the text box – Sami Kuhmonen May 08 '19 at 14:51
  • @SamiKuhmonen: Pls Guide me to do that with some example code and in which event to place it. – D J May 08 '19 at 14:55
  • Check if `textBoxResults.Text.Contains("a")`, if so `textBoxResults.Text.Replace("a", string.Empty)` - quick and dirty, you can do it more pretty your self ;) – nilsK May 08 '19 at 15:09
  • Before displaying it in the textbox, clean/scrub the text through a method and display the result in the textbox instead – Theofanis Pantelides May 08 '19 at 15:59
  • Possible duplicate of [Remove characters from C# string](https://stackoverflow.com/questions/7411438/remove-characters-from-c-sharp-string) – Baddack May 08 '19 at 19:15

3 Answers3

1

You do not need to iterate to every char to be eliminated if you use Regular Expressions.

//Reference to RegularExpressions
using System.Text.RegularExpressions;

...
//Code omitted...
...

//Put the code in TextChanged event
private void TextBoxResults_TextChanged(object sender, EventArgs e)
{
    textBoxResults.Text = Regex.Replace(textBoxResults.Text, "[ab]", "");
}

The "[ab]" string is a RegEx pattern... In this pattern you can put any character or range of characters to be eliminated.

Simple examples:

  • "[a-zA-Z]" you will eliminate any alfabetical character
  • "[^bla]" you will eliminate any character not equal to 'b', 'l' & 'a'.

Here some other examples of patterns:

abc…    Letters

123…    Digits

\d  Any Digit

\D  Any Non-digit character

.   Any Character

\.  Period

[abc]   Only a, b, or c

[^abc]  Not a, b, nor c

[a-z]   Characters a to z

[0-9]   Numbers 0 to 9

\w  Any Alphanumeric character

\W  Any Non-alphanumeric character

\s  Any Whitespace

\S  Any Non-whitespace character

More info here:

Microsoft Regex.Replace reference

Ignotus
  • 564
  • 5
  • 17
-1

Based solely on the info you provided, my suggestion is to just use

textBoxResults.Text = textBoxResults.Text.Replace("<unwanted char>", "")

This replaces any unwanted char in the string with an empty string, de facto deleting them. Bear in mind that this requires a call to Replace(...) for each unwanted character.

StackLloyd
  • 409
  • 2
  • 9
  • :To which event should I add this?? Is TextChanged ok?? – D J May 08 '19 at 15:17
  • 1
    I think TextChanged is ok. Remember to reassign the result to the TextBox, as `Replace()` does not change the string it is called on, but rather returns a new one: `textBox.Text = textBox.Text.Replace(...)`. If, for some reason, you specifically want to target text while it's being typed, use KeyUp, but it won't work with copy-pastes. – StackLloyd May 08 '19 at 15:20
  • Ok. Just be on this question. Ill try thi moment and mark this as answer. – D J May 08 '19 at 15:23
-1

Not exactly what all of you said, but something grouped after googling a bit.

string ToBeReplaceCharacters = @"a;b";
foreach(var RepChar in ToBeReplaceCharacters)
{
  textBox1.Text = textBox1.Text.Replace(RepChar.ToString(), "");
}

What Stackloyd said is ok for single character but not multiple characters to be replaced. The above code fulfils my demand.

Thank You All!

D J
  • 845
  • 1
  • 13
  • 27
  • I think that you can use Regular Expressions for a more elegant and compact solution... – Ignotus Aug 25 '19 at 14:43
  • @Ignotus I was a rookie programmer back then to know much about regex and how easy and compact it is. Now that I have quite some experience with programming, regex is extremely easy and useful! – D J Oct 01 '21 at 14:59