日期:2014-05-19  浏览次数:21028 次

字符串截取问题,请问如何获取首个非英文字母的位置
我在前台传入sql的条件诸如:aaa> '100 '   and     bbb <= '200 '
aaa和bbb是两个字段名
我在后台获取到这个条件后,还想获取这两个字段名,请问从上面:aaa> '100 '   and     bbb <= '200 '这个字符串中该如何截取?谢谢大家

------解决方案--------------------
把aaa和bbb也该成变量作为参数传递不就可以了
------解决方案--------------------
string aaa;
string bbb
string sql;
sql= aaa+ " <100 and "+bbb+ "> 100 "
------解决方案--------------------
public static bool GetCondition(string strSentence, out double outCondition)
{
Match res = Regex.Match(strSentence, @ "(? <=[ <> =]+? ')\d+\.?\d*(?= ') ");
if (res.Success)
{
if (double.TryParse(res.Value,out outCondition))
{
return true;
}
}
outCondition = 0;
return false;
}

然后使用方法如下:
string strSource = "aaa> '100 ' and bbb <= '200 ' ";
double dCondition;
if (GetCondition(strSource, out dCondition))
{
//dCondition可以用了
}
------解决方案--------------------
回头看了以下,自己都觉得无法使用。修改:
public static bool GetCondition(string strSentence,string strField, out double outCondition)
{
Match res = Regex.Match(strSentence, @ "(? <= " + strField + @ "[ <> =]+? ')\d+\.?\d*(?= ') ",RegexOptions.IgnoreCase);
if (res.Success)
{
if (double.TryParse(res.Value,out outCondition))
{
return true;
}
}
outCondition = 0;
return false;
}

使用:
private void button5_Click(object sender, EventArgs e)
{
string strSource = "aaa> '100 ' and bbb <= '200 ' ";
double dCondition;
if (GetCondition(strSource, "aaa ", out dCondition))
{
dCondition++;
}
if (GetCondition(strSource, "BBB ", out dCondition))
{
dCondition++;
}
}