Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to search a string for strings

I'm trying to find the easiest way to search a string for an array of possible strings. I know the easy way to do this for characters is to use myString.IndexOfAny(charArray). But how what if I'd like to search my string for strings and not just characters? Are there any .net tricks or methods that make this easier?

Basically, I'd like to do something like this:

string myName = "rahkim";
string[] names = new string[] {"joe","bob","chris"};

if(myName.IndexOfAny(names) >= 0)
{
      //success code//
}

I know there are ways to do this with loops, etc. But I was hoping for something inherent in the framework.

like image 843
rahkim Avatar asked Apr 23 '09 18:04

rahkim


People also ask

How will you locate a string within a string?

Search for string inside another string - strstrThe function strstr returns the first occurrence of a string in another string.

How do I find a string in a string python?

Python String find() method returns the lowest index or first occurrence of the substring if it is found in a given string. If it is not found, then it returns -1. Parameters: sub: Substring that needs to be searched in the given string.

How do you find strings?

String find is used to find the first occurrence of sub-string in the specified string being called upon. It returns the index of the first occurrence of the substring in the string from given starting position.

How do I search for a word in a string?

To find a word in the string, we are using indexOf() and contains() methods of String class. The indexOf() method is used to find an index of the specified substring in the present string. It returns a positive integer as an index if substring found else returns -1.


3 Answers

You should define if you want to to find equal strings or search for a matching substring. Both ways are easy pre-LINQ and with LINQ.

string myName = "rahkim";
string[] names = new string[] { "joe", "bob", "chris" };
Equal Strings, LINQ
bool contains = names.Contains(myName);
Equal Strings, Pre-LINQ
bool contains = new List<string>(name).Contains(myName);
Substrings, LINQ
bool contains = names.Any(name => name.Contains(myName));
Substring, Pre-LINQ
bool contains = false;
foreach(string name in names)
  if (name.Contains(myName))
    contains = true;
like image 195
Samuel Avatar answered Jan 03 '23 16:01

Samuel


If anyone else found this while trying to search for a .Net method like String.IndexOfAny(String[]), this is my solution:

C#

public int IndexOfAny(string test, string[] values)
{
int first = -1;
foreach (string item in values) {
    int i = test.IndexOf(item);
    if (i >= 0) {
        if (first > 0) {
            if (i < first) {
                first = i;
            }
        } else {
            first = i;
        }
    }
}
return first;
}

VB

Public Function IndexOfAny(test As String, values As String()) As Integer
        Dim first As Integer = -1
        For Each item As String In values
            Dim i As Integer = test.IndexOf(item)
            If i >= 0 Then
                If first > 0 Then
                    If i < first Then
                        first = i
                    End If
                Else
                    first = i
                End If
            End If
        Next
        Return first
    End Function

You can do a LastIndexOfAny(String[]) by just switching the

i < first 

to

i > first
like image 42
Laurence Avatar answered Jan 03 '23 14:01

Laurence


You can (also) use the static IndexOf method of the Array class:

bool hasName = Array.IndexOf(names, myName) > -1;
like image 25
Pat Avatar answered Jan 03 '23 14:01

Pat