So i have this C# code:
static void Main(string[] args)
{
string @string = "- hello dude! - oh hell yeah hey what's up guy";
Console.WriteLine(String.Join(".", @string.GetSubstringsIndexes("he")));
Console.Read();
}
partial class that adds an extension "GetSubstringsIndexes" method:
partial class StringExtension
{
public static int[] GetSubstringsIndexes(this string @string, string substring)
{
List<int> indexes = new List<int>(@string.Length / substring.Length);
int result = @string.IndexOf(substring, 0);
while (result >= 0)
{
indexes.Add(result);
result = @string.IndexOf(substring, result + substring.Length);
}
return indexes.ToArray();
}
}
What i would want it to be like, is a lambda expression in the parameters brackets of a String.Join method instead of calling a function i wrote.
I mean, i would just want not to write this function and THEN call it, but to write a lambda expression to use only once!
Example of how i would want it to look like:
static void Main(string[] args)
{
string @string = "- hello dude! - oh hell yeah hey what's up guy";
Console.WriteLine(String.Join(".", () => {List<int> ind = new List<int>()..... AND SO ON...} ));
Console.Read();
}
Well, actually, I've just realized (while writing this question) that for this kind of a situation it is unnecessary, because my GetSubStringsIndexes method is too big. But imagine if it were a short one.
Just tell me whether or not it is possible to do something like that, and if it is possible, please, tell me how!
Edit:
I've done it and that's how it looks like:
Console.WriteLine(String.Join(".", ((Func<int[]>)
( () =>
{
List<int> indx = new List<int>();
int res = @string.IndexOf("he", 0);
while (res >= 0)
{
indx.Add(res);
res = @string.IndexOf("he", res + "he".Length);
}
return indx.ToArray();
}
))()));