1

I'm working in C# and I have 2 textboxes. If the user enters text in the first box and presses a button the text copy's into text box 2. I've now made another text box and I'm wanting it to show all the strings that contain @ if the user has entered them.
For example,
User enters "Hi there @joey, i'm with @Kat and @Max"
Presses button
"Hi there @joey, i'm with @Kat and @Max" appears in textbox 2
and @joey @Kat @Max appear in text box 3.

Just not sure how i'd do the last part.
Any help thanks! ............................................................................................. Okay, so I decided to go of and try to learn how to do this, I've got this so far

string s = inputBx.Text;
             int i = s.IndexOf('@');

            string f = s.Substring(i);
            usernameBx.Text = (f);

This works however it prints all the words after the word with the @ symbol. So like if I was to enter "Hi there @joey what you doing with @kat" it would print @joey what you doing with @kat instead of just @joey and @kat.

2
  • You have not used any of the listed answers methods? Commented Apr 17, 2012 at 12:07
  • Through trial and error yes, I tried using string.contains but it throws and error saying can't convert bool to string. Commented Apr 17, 2012 at 12:12

6 Answers 6

3

I would Split the string into an array then use string.contains get the items that contain the @ symbol.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. I'd give you another couple of upvotes if I could, for pointing in the right direction instead of providing the full answer for what is obviously a newbie question (if it's not homework). Nicely done.
2

A simple RegEx to find words that begin with @ should be sufficient:

string myString = "Hi there @joey, i'm with @Kat and @Max";
MatchCollection myWords = Regex.Matches(myString, @"\B@\w+");
List<string> myNames = new List<string>();

foreach(Match match in myWords) {
    myNames.add(match.Value);
}

Comments

0

You could use regular expressions to find the words you search for.

Try this regex

@\w+

Comments

0
var indexOfRequiredText = this.textBox.Text.IndexOf("@");

if(indexOfRequiredText > -1)
{
    // It contains the text you want
}

1 Comment

The op wants to get all of the names containing @, this will only get the first one.
0

Maybe not the neatest soultion. But something like this:

string str="Hi there @joey, i'm with @Kat and @Max";
var outout= string.Join(" ", str
               .Split(' ')
               .Where (s =>s.StartsWith("@"))
               .Select (s =>s.Replace(',',' ').Trim()
            ));

Comments

0

A Regex would work well here :

var names = Regex.Matches ( "Hi there @joey, i'm with @Kat and @Max", @"@\w+" );

foreach ( Match name in names )
    textBox3.Text += name.Value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.