ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ContainsAny

Checks if a given string contains any of the characters in the passed array of characters.

Source

public static bool ContainsAny(this string theString, char[] characters)
{
	foreach(char character in characters)
	{
		if (theString.Contains(character.ToString()))
		{
			return true;
		}
	}
	return false;
}

Example

char[] invalidFileNameCharacters = Path.GetInvalidFileNameChars();

if (newFileName.ContainsAny(invalidFileNameCharacters))
{
	MessageBox.Show("File name contains invalid characters.");
}

Author: Jonas Butt

Submitted on: 24 aug. 2008

Language: C#

Type: System.String

Views: 10712