0

I need to check if a certain filename contains the user speficied substring, but I can't seem to get the syntax correct (I'm new to C#). I am expected to use the asterisks, but I'm unsure how it should be formatted (see my code below).

if (fileInfo[i].Name == ("{0} * . * ", partialName) || fileInfo[i].Name == (" * {0}. * ", partialName))

1 Answer 1

3

Try using the Contains method from string, for sample:

if (fileInfo[i].Name.Contains(partialName)) 
{
   // your code
}

If you want to remove the extension from name, try using the Path static class and GetFileNameWithoutExtension method, for sample:

string fileName = System.IO.Path.GetFileNameWithoutExtension(fileInfo[i].Name);

if (fileName.Contains(partialName)) 
{
   // your code
}

Now, if you want to ignore case sensitive strings, try using the IndexOf method, for sample:

if (fileInfo[i].Name.IndexOf(partialName, StringComparison.OrdinalIgnoreCase) >= 0)
{
   // your code
}

or apply the Path.GetFileNameWithoutExtension to use on IndexOf.

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

1 Comment

I have to use * 's, plus, using .Contains also checks the file extension, as that is contained in the string.

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.