判断字符串"1,2,34,24,56"中包含"2"的若干种方法

using System;
using System.Collections.Generic;
        static class Program
        {
            public static bool IsContain(string []str,int i)
            {
                if (i==str.Length)
                {
                    return false;
                }
                i++;
                if (str[i-1]=="2")
                {
                    return true;
                }
                else
                {
                    return IsContain(str, i);
                }
            }
            static void Main()
            {
                //BaseB baseb = new BaseB();
                //baseb.MyFun();
                //判断字符串"1,2,34,24,56"中包含2的若干种方法
                string str = "1,2,34,24,56";
                //方法一
                if (str.Split(',').Where(o => o == "2").Count() > 0)
                {
                    Console.WriteLine("lambda_true");
                }
                else
                {
                    Console.WriteLine("lambda_false");                
                }
                 
	           //方法二循环算法的实现
                string []strDatas = str.Split(new char[] { ',' });
                int count=strDatas.Length;
                for (int i = 0; i < count; i++)
                {
                    if (strDatas[i]=="2")
                    {
                        Console.WriteLine("Loop_true");
                        break;
                    }
                    if (i>=count-1)
                    {
                        Console.WriteLine("loop_false");
                    }
                }
                //方法三递归算法的实现
                if (IsContain(str.Split(','),0))
                {
                    Console.WriteLine("digui_true");
                }
                else
                {
                    Console.WriteLine("digui_false");
                }
 
               //方法四goto语句的实现
 
                int j = 0;
                string[] test = str.Split(',');
                count = test.Length;
                jump_unfinish:  
                if (j<count&&test[j]=="2")
                {
                    Console.WriteLine("goto_true");
                    goto jump_finish;
                }
                if (j>=count)
                {
                    Console.WriteLine("goto_false");
                    goto jump_finish;
                }
                j++;
                goto jump_unfinish;
                jump_finish:      
	
                Console.ReadLine();
            }
        }
    }
posted @ 2013-04-15 07:15  Predator  阅读(203)  评论(0编辑  收藏  举报