[新手提问]c#中对字符串的判断和处理的操作 本帖最后由 oMoWuYan 于 2012-09-02 14:47:32 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这东西别问我们,你自己知道表示什么意思,那就按照你的规则拆分出来就好了,正则可以拆分,普通字符串indexof+substring都可以 string test = @"+90/-5"; float chengji=0; float.TryParse(test,out chengji); if (chengji == 0) { string pattern_com = @"([+-])(\d+(\.\d+)?)"; Regex.Matches(test, pattern_com).Cast<Match>().ToList().ForEach(a => { if (a.Groups[1].Value.Equals("+")) { chengji += float.Parse(a.Groups[2].Value); } else { chengji -= float.Parse(a.Groups[2].Value); } }); } //此时 chengji=85 判断前面没有=号的话Excel.cell[1,1] = "=" + Excel.cell[1,1] 如果string test =“+5又很-9”;应该是报错,实际返回是-4;这个应该处理呢? string test = @"+5又很-9"; float chengji = 0; float.TryParse(test, out chengji); if (chengji == 0) { string pattern_match = @"^([+-]\d+/?)+$"; if (Regex.IsMatch(test, pattern_match)) { string pattern_com = @"([+-])(\d+(\.\d+)?)"; Regex.Matches(test, pattern_com).Cast<Match>().ToList().ForEach(a => { if (a.Groups[1].Value.Equals("+")) { chengji += float.Parse(a.Groups[2].Value); } else { chengji -= float.Parse(a.Groups[2].Value); } }); } else { MessageBox.Show("格式错误"); } } 定义一个函数专门来处理这个: private string GetStrExpression(string str){ str = str.Trim(); string[] ops = str.Split('/'); int op1Int = int.Parse(ops[0].Trim()), op2Int = int.Parse(ops[1].Trim());//获得2个操作数(整型) string op2sys = op2Int > 0 ? "+" : ""; string result = string.Format("{0}{1}{2}={3}", op1Int, op2sys, op2Int, op1Int + op2Int); return result; }测试 string dd= GetStrExpression("+98/-25");结果 dd="98-25=85" 在比较大的cs文档中经常为了找编辑的位置很费时,哪位有好办法 保存 如何给指定Ip地址发送信息呢?高手指教 XML文件比较 新手求教 如何获得窗体的宽度呢? 一个VS2003快捷键的问题,如何选择一列?谢谢大家了 如果在用户控件里面触发父窗口的事件啊? 数据集中数据表与文本框绑定时的GetChanged()问题 C# 实现重新启动计算机? wpf中包含多个子窗口时最小化托盘的问题 C# 串口丢失的异常 catch的括号里写什么
float chengji=0;
float.TryParse(test,out chengji);
if (chengji == 0)
{
string pattern_com = @"([+-])(\d+(\.\d+)?)";
Regex.Matches(test, pattern_com).Cast<Match>().ToList().ForEach(a => {
if (a.Groups[1].Value.Equals("+"))
{
chengji += float.Parse(a.Groups[2].Value);
}
else
{
chengji -= float.Parse(a.Groups[2].Value);
}
});
}
//此时 chengji=85
Excel.cell[1,1] = "=" + Excel.cell[1,1]
如果string test =“+5又很-9”;
应该是报错,实际返回是-4;
这个应该处理呢?
float chengji = 0;
float.TryParse(test, out chengji);
if (chengji == 0)
{
string pattern_match = @"^([+-]\d+/?)+$";
if (Regex.IsMatch(test, pattern_match))
{
string pattern_com = @"([+-])(\d+(\.\d+)?)";
Regex.Matches(test, pattern_com).Cast<Match>().ToList().ForEach(a =>
{
if (a.Groups[1].Value.Equals("+"))
{
chengji += float.Parse(a.Groups[2].Value);
}
else
{
chengji -= float.Parse(a.Groups[2].Value);
}
});
}
else
{
MessageBox.Show("格式错误");
}
}
private string GetStrExpression(string str){
str = str.Trim();
string[] ops = str.Split('/');
int op1Int = int.Parse(ops[0].Trim()),
op2Int = int.Parse(ops[1].Trim());//获得2个操作数(整型)
string op2sys = op2Int > 0 ? "+" : "";
string result = string.Format("{0}{1}{2}={3}", op1Int, op2sys, op2Int, op1Int + op2Int);
return result;
}
测试 string dd= GetStrExpression("+98/-25");
结果 dd="98-25=85"