去除字符串前后的逗号:
如:1,2,5, 变成 1,2,5
,,,1,2,5, 变成 1,2,5
,,,,,,, 变成
View Code
public String trim(String str)
{
int len = str.Length;
int st = 0;
char[] val = str.ToCharArray();
int i = 0;
while ((st < len) && (val[st] <= ','))
{
i++;
st++;
}
while ((st < len) && (val[len - 1] <= ','))
{
i++;
len--;
}
if (st == len)
return "";
return ((st > 0) || (len < str.Length)) ? str.Substring(st, str.Length - i) : str;
}
验证特殊字符:
View Code
private bool IsNatural_Number(string str)
{
System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex("[^%&'.。,,;=?$]+$");
return reg1.IsMatch(str);
}