我有一个字符串,可以有任何句子,但在该字符串中的某个地方将是@符号,后跟一个附加的单词,有点像你在某些网站上看到的@username.
也许字符串是"嘿你好吗"或者是"@john嘿你好吗".
如果字符串中有一个"@",我想把它后面的内容拉到它自己的新字符串中.
在这种情况下,我怎么能把"john"拉成一个不同的字符串,这样我理论上可以通知这个人他的新消息?我正在尝试使用string.contains或.replace,但我很新并且很难过.
这个btw在c#asp.net中
以下是没有正则表达式的方法:
string s = "hi there @john how are you";
string getTag(string s)
{
int atSign = s.IndexOf("@");
if (atSign == -1) return "";
// start at @, stop at sentence or phrase end
// I'm assuming this is English, of course
// so we leave in ' and -
int wordEnd = s.IndexOfAny(" .,;:!?", atSign);
if (wordEnd > -1)
return s.Substring(atSign, wordEnd - atSign);
else
return s.Substring(atSign);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
290 次 |
| 最近记录: |