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?

Improve company productivity with a Business Account.Sign Up

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.