CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2005
    Posts
    2

    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;
    }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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?

  3. #3
    Join Date
    Jul 2015
    Posts
    12

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured