Search for multiple lines in a line

In this line, it is easy to find the first occurrence of a substring like this:

int position = "01234".IndexOf ("23"); // returns 2 

I want to find the first occurrence of any of several possible lines:

 var options = new [] {"77", "34", "12"}; int position = "01234".ImaginaryIndexOf (options); // should return 1 

Such a function does not seem to exist on the .NET platform. Did I miss it?

Edit: To clarify, I am looking for a method that works well even for large inputs and unevenly distributed parameters. Imagine something like

 var options = new [] {"x", "y"}; new String ('x', 1000*1000).IndexOf (options) 
+5
source share
6 answers

There is no built-in method that I know of.

But for this you can iterate over all options and for each count IndexOf . Then extract a minimum that is not -1 (from "not found"):

 int position = options.Select(o => "01234".IndexOf(o)) .OrderBy(i =>i).FirstOrDefault(i=> i != -1); 

Or instead of sorting (which is O(nlogn) ), find the minimum ( O(n) ):

 int position = options.Select(o => "01234".IndexOf(o)) .Where(i => i != -1).DefaultIfEmpty(-1).Min(); 

As for editing, you can consider building and an array of suffix trees - the array contains m elements, where m is the individual sum of the first letters of your options words. As a general example:

if the parameters are "some", "word", "something", "other" , then you built:

  0 1 2... +-----------------------+ | s | w | o | +- | ------ | ------ | -+ oot | | | mrh | | | ede / \ | | $ tr $ | | ... $ 

Then you repeat your line and for each letter you check to see if it is in an array. If not continue further. If so, you can go deeper into the nested tree and check the next letter of the string compared to the next level in the tree. If at the end of the main line you have not reached any of $ , then there is no options element in the text. Of course, you can have an array like HashSet<T> to improve the search for the first letter of the word.

+2
source

Maybe something like that;

 var options = new[] { "77", "34", "12" }; var position = options.Select(x => "01234".IndexOf(x)) .Where(x => x > -1).OrderBy(x => x).DefaultIfEmpty(-1) .FirstOrDefault(); 

You can define the extension of the string;

 public static class StringExtensions { public static int ImaginaryIndexOf(this string str,IEnumerable<string> options) { return options.Select(x => str.IndexOf(x)) .Where(x => x > -1).OrderBy(x => x) .DefaultIfEmpty(-1).FirstOrDefault(); } } 

Then;

 var options = new[] { "77", "34", "12" }; "01234".ImaginaryIndexOf(options); 
+2
source

Hi, we can solve this using for loop or using regex. Try the following code that I used for the loop to find the index of the first occurrence of the string.

 var options = new[] { "77", "34", "12" }; for (int i = 0; i < options.Length; i++) { //MessageBox.Show(options[i].ToString); int p 

 var options = new[] { "77", "34", "12" }; for (int i = 0; i < options.Length; i++) { int position = "770123473673412".IndexOf(options[i].ToString()); MessageBox.Show(position.ToString()); } 
0
source

Pretty easy with some LINQ. The hard part handles -1 ... but if you understand unsigned and signed integers, you can solve this problem using translation.

 static class StringExtensions { public static int ImaginaryIndexOf(this string input, IEnumerable<string> searchFor) { return (int)searchFor .Select ( s => (uint)input.IndexOf(s) ) .Min(); } } 

Try using DotNetFiddle .

0
source

As an alternative, you can use Regex

 string input = "01234"; var rgex = new System.Text.RegularExpressions.Regex("(77|34|12)"); var match = rgex.Match(input); int position = match.Success ? match.Index : -1; 
0
source

Hi, we can solve this using for loop or using regex. Try the following code that I used for the loop to find the index of the first occurrence of the string.

 var options = new[] { "77", "34", "12" }; for (int i = 0; i < options.Length; i++) { int position = "770123473673412".IndexOf(options[i].ToString()); MessageBox.Show(position.ToString()); } 
-2
source

Source: https://habr.com/ru/post/1274749/


All Articles