none
c# Remove all text before a specific character in textBox1.Text ? RRS feed

  • Question

  • Hello everyone,
    I need the code to remove all text before a specific character in textBox1.Text.

    Text before a specific character can be anything.

    Specific character = ":"

    for example, this is code to remove anything after a specific character is:

    private void removeafter() { String orgText = textBox1.Text; int i = orgText.LastIndexOf(":"); if (i != -1) { textBox1.Text = orgText.Remove(i); }

    }

    But I need the reverse (removing everything before of a specific character).
    Thanks in advance.



    • Edited by nextb88 Thursday, August 30, 2018 5:55 PM
    Thursday, August 30, 2018 5:53 PM

Answers


  • I need the code to remove all text before a specific character in textBox1.Text.

    You mean you want something like this?

    string str = "12345:67890";
    int idx = str.IndexOf(':');
    if(idx != -1) str = str.Remove(0,idx);

    - Wayne


    • Edited by WayneAKing Thursday, August 30, 2018 8:17 PM colon, not semicolon
    • Marked as answer by nextb88 Thursday, August 30, 2018 9:03 PM
    Thursday, August 30, 2018 8:15 PM

All replies


  • I need the code to remove all text before a specific character in textBox1.Text.

    You mean you want something like this?

    string str = "12345:67890";
    int idx = str.IndexOf(':');
    if(idx != -1) str = str.Remove(0,idx);

    - Wayne


    • Edited by WayneAKing Thursday, August 30, 2018 8:17 PM colon, not semicolon
    • Marked as answer by nextb88 Thursday, August 30, 2018 9:03 PM
    Thursday, August 30, 2018 8:15 PM
  • Thanks Wayne,
    You helped me to understand and solve the problem.
    Now it looks like this. That's what I needed.

     private void removebefore()
            {
                String orgText = textBox1.Text;
                int i = orgText.IndexOf(":");
                if (i != -1)
                {
                    textBox1.Text = orgText.Remove(0, i);
                }
            }

    Thursday, August 30, 2018 9:03 PM