2012-04-17 92 views
1

我正在C#中工作,我有2个文本框。如果用户在第一个框中输入文本并按下按钮,则文本副本将进入文本框2.现在,我创建了另一个文本框,并且我希望它显示包含@的所有字符串(如果用户输入了它们)。
例如,
用户输入“您好@joey,我是与@Kat和@Max”
压力机按钮
“您好@joey,我是与@Kat和@Max”出现在文本框2
@joey @Kat @Max出现在文本框中输入3C#如果字符串包含

只是不知道我怎么会做最后一部分。
任何帮助谢谢! ................................................. ............................................ 好吧,所以我决定去,并尝试学习如何做到这一点,我有这个迄今为止

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

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

然而,这工作也将打印@符号字之后的所有单词。所以,如果我想输入“Hi there @joey你在@kat上做什么” 它会打印@joey你用@kat而不是@joey和@kat所做的。

+0

请参见新编辑 – user1300788 2012-04-17 11:50:59

+0

你有没有使用任何列出的答案方法? – TheRealTy 2012-04-17 12:07:55

+0

通过试验和错误是的,我尝试使用string.contains,但它引发和错误说不能将bool转换为字符串。 – user1300788 2012-04-17 12:12:39

回答

3

我会Split串入一个数组,然后使用string.contains得到包含@符号的项目。

+0

+1。如果可以的话,我会再给你一些upvotes,指出正确的方向,而不是为显然是新手问题(如果它不是家庭作业)提供完整的答案。做得很好。 – 2012-04-17 11:13:17

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

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

该op想要获得所有包含@的名称,这只会得到第一个。 – TheRealTy 2012-04-17 12:08:35

0

您可以使用正则表达式来查找您要搜索的单词。

试试这个正则表达式

@\w+ 
0

也许不是最整洁soultion。但这样的事情:

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() 
      )); 
0

一个正则表达式将工作做好的位置:

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

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

一个简单的正则表达式来查找与@开始应该是足够的话:

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

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