当前位置:编程学习 > C#/ASP.NET >>

在.net中 ,我要判断 一个字符串 不能为0,或者00,或者000 ...就是不能不能全部为 0 怎么做啊 ?????????

我要判断 一个字符串 不能为0,或者00,或者000 ...就是不能不能全部为 0

怎么做啊 ??? --------------------编程问答-------------------- if(a=="0"||a=="00"||a=="000")
  不能为0
--------------------编程问答-------------------- 转成数值型判断不行么? --------------------编程问答-------------------- 方法很多...比如...

str.Trim('0').Length!=str.Length

str.Count(c=>c!='0')>0

str.Intersect(new char[]{'0'}).Count()==0

等等... --------------------编程问答-------------------- str.Trim('0').Length!=str.Length

嗯...这个方法不对... --------------------编程问答-------------------- 还有...

str.All(c => c != '0')

--------------------编程问答--------------------   private void Judem(string str)
    {
        int count=str.Length;
        int num = 0;
        foreach (char c in str)
        {
            if (c == Convert.ToChar("0"))
            {
                num++;
            }
        }
        if (count == num)
        {
            Response.Write("不能全部为零");
            Response.End();
        }
        else
        {
            Response.Write("true");
            Response.End();
        }
    } --------------------编程问答-------------------- 0 的aiscii 为32
检查所有字符的aiscii 是否为32 --------------------编程问答-------------------- 用正则式 --------------------编程问答--------------------


public bool CheckAllZero(string strValue)
        {
            int i;
            if (int.TryParse(strValue, out i))
            {
                if (i == 0)
                {
                    return false;
                }
            }
            return true;
        }

--------------------编程问答--------------------

 void Main()
{
   var list=new List<string>{"1100","0000","00","001"};
   list.ForEach(str=>Console.WriteLine(string.Format("{0}验证结果:\t{1}",str,Check(str))));
}

 //提供3种方法  都可以
 bool Check(string str)
 { 
   return str.Replace("0","").Trim().Length==0;
   //return str.ToCharArray().Except(new char[]{'0'}).Count()==0;
   //return str.ToCharArray().Count(s=>s=='0')==str.Length;  
 }
/*
1100验证结果: False
0000验证结果: True
00验证结果: True
001验证结果: False
*/
--------------------编程问答--------------------

private static Regex RTel = new Regex(@"^[0]*$");

 if(RTel.IsMatch(TextBox.Text.Trim()))
            {
                lblMessage.Text = "不能为0";
            }
--------------------编程问答--------------------

 public bool CheckAllZero(string strValue)
        {
            if (!string.IsNullOrEmpty(strValue))
            {
                if (strValue.IndexOf('.') < 0)
                {
                    int i; 
                    if (int.TryParse(strValue, out i))
                    {
                        if (i == 0)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
--------------------编程问答--------------------

static void Main(string[] args)
        {
            string[] strs = { "00", "0000", "000200" };
            foreach (string item in strs)
            {

                Console.WriteLine(item.Split('0').Length == item.Length+1 ? "True" : "False");
            }
            Console.Read();
        }
--------------------编程问答-------------------- static void Main(string[] args)
        {
            string[] strs = { "00", "0000", "000200" };
            
            var s = from ss in strs where ss.Trim('0').Length == 0 select ss;
            foreach (string item in s)
            {

                Console.WriteLine(item);
            }

            Console.Read();
        } --------------------编程问答-------------------- str.Trim('0').Length != 0 --------------------编程问答-------------------- 用Replace把“0”换成“”

在看长度是否为0即可。 --------------------编程问答-------------------- 直接把输入的值跟0相加,如果结果是0,就代表输入的是0或者多个0


<html>
<title>测试非法字符_hzsasheng
</title>
<head>
<script>
function check(){
var t = document.getElementById("txtTest").value;
if( t + 0 == 0 )
alert("非法的字符");
else
alert("正确的字符");
}
</script>
</head>
<input type="text" id="txtTest">
<input type="button" value=" check " onclick="check()">
</body>
</html>
--------------------编程问答--------------------

int.Parse("00000")==0?"OK":"NO"

--------------------编程问答-------------------- int result=0;
if (result.ToString().Replace("0","")=="")
把0全部替换成空格.
如果最后生么都不剩下.就表示全部都是0 --------------------编程问答--------------------
引用 16 楼 hua456 的回复:
用Replace把“0”换成“”

在看长度是否为0即可。
这个最简单 --------------------编程问答--------------------
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,