Share via


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

Question

Thursday, August 30, 2018 5:53 PM

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.

All replies (2)

Thursday, August 30, 2018 8:15 PM âś…Answered | 1 vote

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

Thursday, August 30, 2018 9:03 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);
            }
        }