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.

user1300788
  • 181
  • 2
  • 17

6 Answers6

3

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

TheRealTy
  • 2,409
  • 3
  • 22
  • 32
  • +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. – Ken White Apr 17 '12 at 11:13
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);
}
James Hill
  • 60,353
  • 20
  • 145
  • 161
0
var indexOfRequiredText = this.textBox.Text.IndexOf("@");

if(indexOfRequiredText > -1)
{
    // It contains the text you want
}
Marco
  • 56,740
  • 14
  • 129
  • 152
Michael Ciba
  • 561
  • 3
  • 6
0

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

Try this regex

@\w+
juergen d
  • 201,996
  • 37
  • 293
  • 362
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()
            ));
Arion
  • 31,011
  • 10
  • 70
  • 88
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;
Mongus Pong
  • 11,337
  • 9
  • 44
  • 72