有一个字符串数组,例如ada())ad((),dad(),dadsa,我要算出总共有多少个字符,其中当遇到“(”“)”其后面的都不算到字符中来,当遇到“,”时又开始算,也就是说从(或者)到,中得都不算到字符数中来,请问如何从(或者)到,的跳转!最好能够把代码写出来看看。急!谢谢!

解决方案 »

  1.   

    比如说有一个数组中的元素是这样的adaf()dad)(,dafd)(,fdafd,现在的问题是()dad)(,和)(,按照我算法是不应该计入总字符串中去的,那我如何实现跳过它们。也就是我标题中说的如何实现使得(到,之间的字符不算在总字符内
      

  2.   

    int count = 0;
                bool go = true;
                string A = "ada())ad((),dad(),dadsa,";
                foreach (char x in A)
                {
                    if (x == '(')   go = false;
                    if (x == ',')
                    {
                        go = true;
                        continue;
                    }
                    if(go)          count++;
                }
      

  3.   

    给你段代码分析一下
    orgId=orgId.Substring(0,orgId.Length-1);
    string temp=string.Empty;
    string []orgItem=orgId.Split(',');
    for(int i=0;i<orgItem.Length;i++)
    {
    temp=temp+"'"+orgItem[i]+"',";
    }
    orgId=temp.Substring(1,temp.Length-3);
    return orgId;
      

  4.   

    string str = "ada())ad((),dad(),dadsa" ;
    int totalLength = str.Length ;

    int result = totalLength ;
    bool target1 = false ;
    bool target2 = false ;
    int target1Position = 0 ;
    for( int i = 0 ; i < totalLength ; i ++ )
    {
    switch( str.Substring( i , 1 ) )
    {
    case "(" : 
    if( !target1 )
    {
    target1Position = i ;
    target1 = true ;
    }
    break ;
    case ")":
    if( !target1 )
    {
    target1Position = i ;
    target1 = true ;
    }
    break ;
    case "," :
    if( !target2 && target1 )
    target2 = true ;
    break ;
    default:break ;
    }
    if( target1 && target2 )
    {
    result -= ( i - target1Position ) ;
    target1 = false ;
    target2 = false ;
    }
    }
    Label1.Text = result.ToString() ;
      

  5.   

    如果考虑某个括号并没有和它对应的逗号时(如下面的最后一个括号),应该用下面的代码:
                string s = "ada())ad((),dad(),dadsa,(aa";
                int count = 0;
                bool sign=false;            for (int i = 0; i < s.Length; i++)
                {
                    char c = s[i];
                    if (!sign && (c == '(' || c == ')' || c == '(' || c == ')'))
                    {
                        sign = true;
                    }                if ((s.LastIndexOf(',')<i || c == ',') && sign == true)
                    {
                        sign = false;
                        continue;
                    }                if (!sign && c != ',')
                    {
                        count++;
                    }
                }
    输出:13