欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#正则检测字符串是否字母数字混编的方法

程序员文章站 2022-10-15 16:40:31
本文实例讲述了c#正则检测字符串是否字母数字混编的方法。分享给大家供大家参考。具体如下: using system.text; using system.tex...

本文实例讲述了c#正则检测字符串是否字母数字混编的方法。分享给大家供大家参考。具体如下:

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
 }
}

希望本文所述对大家的c#程序设计有所帮助。