Solved

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

Posted on 2013-01-09
3
364 Views
Last Modified: 2013-01-09
How can I add text after hyphon in textbox:

kjhkjhkjh - add text here??
0
Comment
Question by:pauledwardian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38761317
How about:

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

Open in new window

0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 38761358
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
 

Author Closing Comment

by:pauledwardian
ID: 38761384
Perfect!
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question