使用正则表达式来进行筛选,正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。
void Start () {
string str="R2f3456";
Debug.Log("AdjustString:" + AdjustString(str, 5, 10));
}
bool AdjustString(string strMe,int minL,int maxL)
{
bool result = false;
//string pattern = @"^[0-9]*$"; //所有字符必须为数字
string pattern = @"^[a-zA-Z0-9]*$"; //所有字符必须为字母或数字
//string pattern = @"^[a-z0-9]*$"; //所有字符小写字母或数字
//string pattern = @"^[A-Z][0-9]*$"; //以大写字母开头,后面的都是数字
//string pattern = @"^\d{3}-\d{2}$"; //匹配 333-22 格式,三个数字加-加两个数字
if (strMe.Length>=minL &&strMe.Length<=maxL) //限制字符串长度再minL和maxL之间
{
if(System.Text.RegularExpressions.Regex.IsMatch(strMe, pattern))
{
result = true;
Debug.Log("格式正确");
}
else
{
result = false;
Debug.Log("格式不满足要求");
}
}else
{
Debug.Log("长度不符合规定");
}
return result;
}版权声明:本文为qq_38506712原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。