日期:2014-05-20  浏览次数:20963 次

截取字符串然后再判断其中是否有大于50的值,如果有报错
比如字符串是:‘2, ,12,31,27,54,0,,6,56,’
怎么做?就这么多分了,

------解决方案--------------------
C# code


string str="2, ,12,31,27,54,0,,6,56,";
String[] array=str.Split(new String[1]{","},StringSplitOptions.RemoveEmptyEntries);
foreach(str s in array)
  if(Convert.ToInt32(s)>50)
  {
    Console.WriteLine("大于50.")
    Break;
  }

------解决方案--------------------
C# code


static void Main(string[] args)
        {
            string str = "2, ,12,31,27,54,0,,6,56,";
            String[] array = str.Split(new String[1] { "," }, StringSplitOptions.RemoveEmptyEntries);

            List<int> arrayList = new List<int>();
            foreach (string s in array)
                if (s.Trim() != "")
                    arrayList.Add(Convert.ToInt32(s));

            //搜索数组中是否有大于50的数字,前面将字符串按逗号分隔
            if (arrayList.Where(r => r > 50).Count() > 0)
                Console.WriteLine("error");

            Console.ReadLine();
        }

------解决方案--------------------
探讨
C# code



static void Main(string[] args)
{
string str = "2, ,12,31,27,54,0,,6,56,";
String[] array = str.Split(new String[1] { "," }, StringSplitOptions.Remove……

------解决方案--------------------
正则 \,((5[1-9])|(6-9)[\d])|([1-9][\d]{2,})\, 判断这个即可
------解决方案--------------------
修正下上面的: \,[ ]*((5[1-9])|[6-9][\d])|([1-9][\d]{2,})[ ]*\,

C# code

string value = "2, ,12,31,27,50,0,,6,50,";
MessageBox.Show(System.Text.RegularExpressions.Regex.IsMatch(value, "\\,[ ]*((5[1-9])|[6-9][\\d])|([1-9][\\d]{2,})[ ]*\\,", System.Text.RegularExpressions.RegexOptions.IgnoreCase));

------解决方案--------------------
bool have = "2, ,12,31,27,54,0,,6,56,".Split(',').Where(x => x.Trim() != "").Any(x => Convert.ToInt32(x) > 50);
------解决方案--------------------
探讨

bool have = "2, ,12,31,27,54,0,,6,56,".Split(',').Where(x => x.Trim() != "").Any(x => Convert.ToInt32(x) > 50);

------解决方案--------------------
C# code

string str = "2, ,12,31,27,54,0,,6,56,";
var datainfoList = str.Split(new string[] { "、" }, StringSplitOptions.RemoveEmptyEntries).ToList();
          
 if (datainfoList.Any(t => Convert.ToDouble(t) > 50))
 {
       throw new Exception("输入的数据有误!");
 }