高分请哪位仁兄指教一下,小弟刚学编程。 有一个字符串数组,例如ada())ad((),dad(),dadsa,我要算出总共有多少个字符,其中当遇到“(”“)”其后面的都不算到字符中来,当遇到“,”时又开始算,也就是说从(或者)到,中得都不算到字符数中来,请问如何从(或者)到,的跳转!最好能够把代码写出来看看。急!谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 比如说有一个数组中的元素是这样的adaf()dad)(,dafd)(,fdafd,现在的问题是()dad)(,和)(,按照我算法是不应该计入总字符串中去的,那我如何实现跳过它们。也就是我标题中说的如何实现使得(到,之间的字符不算在总字符内 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++; } 给你段代码分析一下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; 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() ; 如果考虑某个括号并没有和它对应的逗号时(如下面的最后一个括号),应该用下面的代码: 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 请教一个关于stringbuilder的问题 关于DLL的调用问题,高手指点一下。 请教 请教关于Access数据库的表中含保留字的数据绑定问题 刚学习c#,请各位指点! 为什么弹出的总是这一个窗口???????急 在程序中播放电影可以用什么来做 installshield 8+.net制作安装文件后安装的问题(.net1.1) 通过lock(fileName) 防止对文件的并发访问,安全吗?如不安全,用什么代替? 帮帮我吧 axwebbrowser编辑模式下按F5的问题 SQL Server 2005 无法连接到 MSDE 2000,高手相助
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++;
}
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;
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() ;
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