2

Below is my string array :

string[] arr = { 
                 "region1.mp3,region1-sub.mp3,region1-sub1.mp3,region2-sub.mp3", 
                 "region2.mp3,region2-Sub1.mp3",
                 "region3.mp3" 
               };

Below is my value which I am trying to search in above string array and get index:

string searchItem = "region1-sub1.mp3";

This is how I am trying to search but getting -1 (-1 indicates search not found I guess):

int index = Array.FindIndex(arr, t => t == searchItem); // -1

I understand that because my records in string array are comma separated that is why this search is failing.

So any other method which can help me find index without looping and generating new string array?

Expected Output : 0

7
  • have you tried Contains ?? Commented Mar 14, 2017 at 8:20
  • int index = Array.FindIndex(arr, t => t.Split(',').Contains(searchItem)); Commented Mar 14, 2017 at 8:24
  • You have an array of strings, not comma separated values. The computer doesn't know that , has a special meaning here. If you want to search individual values, you have to split the strings first, or use string.Contains or a Regex for better performance Commented Mar 14, 2017 at 8:26
  • 1
    @Learning why do you think that? It's actually faster in most cases because it doesn't require any string manipulations like splitting, and doesn't return any results until you specifically ask for them. It only returns pointers into the original string. IsMatch, not even that Commented Mar 14, 2017 at 11:24
  • 1
    @Learning orders of magnitude at least. Which is why admins use regex in shell scripts instead of manually splitting strings. Part of the reason is that a regex results in a parser purpose built to match a pattern. It can do so just by reading the stream of charactes, it doesn't have to read everything just to split it. That uses significantly less CPU and RAM. Another part is that the results are a collection of indexes to the original string. Only when you request a value with .Value is a string created. This saves CPU and RAM and avoids the penalty of GCing a few thousands strings Commented Mar 14, 2017 at 12:13

3 Answers 3

3

You want to split every string by comma:

int index = Array.FindIndex(arr, t => t.Split(',').Contains(searchItem)); 

This works even if the string doesn't contain a comma.

Sign up to request clarification or add additional context in comments.

5 Comments

Please note that this will only work if searchItem matches exactly. If searchItem="region1-sub1" then the result is -1. If searchItem can be a partial string then use Contains without Split.
@RuardvanElburg: of course, that's how i understood OP's requirement. The audio-files are separated by commas. It also returns the desired result. You can't search ".mp3" with this approach. If that's desired he needs to change the query, f.e.: Array.FindIndex(arr, t => t.Split(',').Select(Path.GetExtension).Contains(".mp3"))
Yeah this worked.Thank you so much for your kind efforts towards helping me.Thanks.Just wanted to ask you 1 thing that 1 user pointed out that using regex will have a good performance in this.What do you think??
@Learning: regex is less efficient than pure string methods. More important: for most people it's also less readable. I try to avoid regex if possible.
@TimSchmelter for direct matching, perhaps. In this case though it would be faster because it would avoid generating temporary strings. As for readable - once you use Splunk, or have to parse large log files, it becomes a lot easier
2

This will you give you your desired output.

int index = Array.FindIndex(arr, t => t.Contains(searchItem));

5 Comments

It's not always will work, but in this case when every file name starts with region and ends with .mp3 you can use simple contains. Otherwise you should split each string to check filenames separately
Agreed. This is just a fix for OP based on the standard format of the elements in his array. He could even drop the ".mp3" suffix from the search item.
If he'll drop file extension from search, then your code will not work. region2 will match both region2.mp3 and region2-sub.mp3
region2 != region2-sub
"region2-sub".Contains("region2") == "region2".Contains("region2")
1

int index = Array.FindIndex(arr, t => t.Contains(searchItem));

This returns 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.