Older languages had the ability to search a string starting left, right or middle. Does C# in unity have this ability? I can’t seem to find the right syntax.
var lewis = "we are what we believe we are";
Console.WriteLine(lewis.IndexOf("we", 0)); // search right from the start for "we". Result: 0
Console.WriteLine(lewis.IndexOf("we", 2)); // skip two letters and search right characters for "we". Result: 12
Console.WriteLine(lewis.LastIndexOf("we", lewis.Length)); // search left from the end for "we". Result: 23
Console.WriteLine(lewis.LastIndexOf("we", 23)); // Starting at index 23, search left for "we". Result: 12
Also
var lewis = "we are what we believe we are";
for (Match regExMatch = System.Text.RegularExpressions.Regex.Match(lewis, "we"); regExMatch.Success; regExMatch = regExMatch.NextMatch())
Console.WriteLine(string.Format("Found text '{0}' at position {1}", regExMatch.Value, regExMatch.Index));
Output is:
Found text ‘we’ at position 0
Found text ‘we’ at position 12
Found text ‘we’ at position 23