What is the quickest and most efficient way of finding a string within another string.
For instance I have this text;
"Hey @ronald and @tom where are we going this weekend"
However I want to find the strings which start with "@".
try this one:
string s = "Hey @ronald and @tom where are we going this weekend";
var list = s.Split(' ').Where(c => c.StartsWith("@"));
Regex will be the best to use.If you are after speed:
string source = "Hey @ronald and @tom where are we going this weekend";
int count = 0;
foreach (char c in source)
if (c == '@') count++;
If you want a one liner:
string source = "Hey @ronald and @tom where are we going this weekend";
var count = source.Count(c => c == '@');
Check here How would you count occurrences of a string within a string?
String str = "hallo world"
int pos = str.IndexOf("wo",0)