C Regular Detection of Alphanumeric Mixing of Strings
- 2021-06-28 09:39:40
- OfStack
An example of how C#regularly detects whether a string is mixed alphanumerically is given.Share it for your reference.Specifically as follows:
using System.Text;
using System.Text.RegularExpressions;
public static class StringExtensions
{
public static bool IsAlphanumeric(this string source)
{
Regex pattern = new Regex("[^0-9a-zA-Z]");
return !pattern.IsMatch(source);
}
}
// EXAMPLE USAGE
class Program
{
static void Main(string[] args)
{
string testString = Console.ReadLine();
if (testString.IsAlphanumeric())
Console.WriteLine("Yep!");
else
Console.WriteLine("Nope!");
Console.ReadKey(); // Wait for key before exiting
}
}
I hope that the description in this paper will be helpful to everyone's C#program design.