C# add text after hyphon (-) in a textbox

How can I add text after hyphon in textbox:

kjhkjhkjh - add text here??
pauledwardianAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
anarki_jimbelConnect With a Mentor Commented:
Try something like:

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.IndexOf("-") > -1)
            {
                this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.IndexOf("-") + 1) + "my added text";
            }
        }

Open in new window


If you need to preserve initial text after "-" (if existed), you may add another substring:


this.textBox1.Text.Substring(this.textBox1.Text.IndexOf("-")+1,
this.textBox1.Text.Length - this.textBox1.Text.IndexOf("-") -1);
0
 
käµfm³d 👽Commented:
How about:

string result = original.Replace("-", "- " + addedText);

Open in new window

0
 
pauledwardianAuthor Commented:
Perfect!
0
All Courses

From novice to tech pro — start learning today.