How to replace characters in a mixed string?
Hi guys! Can anyone please tell me why the code below is not working? Thanks!
private void tipoComboBox_SelectedValueChanged(object sender, EventArgs e)
{
if (tipoComboBox.SelectedIndex == 0)
{
string Str = valorTextBox.Text;
Str = Regex.Replace(Str, @"-", "");
valorTextBox.ForeColor = Color.Blue;
valorTextBox.Text = Str;
}
if (tipoComboBox.SelectedIndex == 1)
{
string Str = valorTextBox.Text;
Regex.Replace(Str, "R$", "-R$");
valorTextBox.ForeColor = Color.Red;
valorTextBox.Text = Str;
}
}
Re: How to replace characters in a mixed string?
You should put a breakpoint on the first if statement in the code and debug it.
At any rate, why are you using Regex replace when String has it's own Replace method?
Re: How to replace characters in a mixed string?
Code:
private void tipoComboBox_SelectedValueChanged(object sender, EventArgs e)
{
if (tipoComboBox.SelectedIndex == 0)
{
string Str = valorTextBox.Text.Replace("-", "");
valorTextBox.ForeColor = Color.Blue;
valorTextBox.Text = Str;
}
if (tipoComboBox.SelectedIndex == 1)
{
string Str = valorTextBox.Text.Replace(R$", "-R$");
valorTextBox.ForeColor = Color.Red;
valorTextBox.Text = Str;
}
}
Try this.