string里分为数字字符和其他字符两大类 我想用 System.Collections.Queue来存储 需要将连续数字字符做为一个整体(即一个整数,但用字符串表示)装进Queue 非数字字符 也做为个整体(即为一个运算符,但用字符表示)装入Queue 并且不改变原string中字符次序 例如:string str = "234+456";
要求Queue中为"234"、"+"、"456" 谁给我个算法呀 谢谢了 :)
要求Queue中为"234"、"+"、"456" 谁给我个算法呀 谢谢了 :)
解决方案 »
- combobox绑定dataset之后,如何动态设定combobox的显示值???
- wpf中的datagrid怎么在CellEditEnding事件中给编辑的单元格赋值?
- C#实现TextBox的自动匹配属性
- .net3.5的listview中的listviewitem怎么响应鼠标事件
- 请教:怎么判断控件的事件属性是否加载了函数
- 高手请指教,怎么向word文档里插入图片?
- VC动态库中指针如何使用C#调用求助
- Yahoo Web api 使用文档
- firefox装个什么插件才能视频聊天
- Dategrid增加行问题,高分相送
- 关于oleDbDataAdapter的一个弱弱问题
- regex.match的一个问题,对regex.match比较了解的请进
然后在這里的時候就好辦多了﹕string.split()搞定.
當然如果沒有辦法在客戶端事先處理﹐再考慮下面的方法:
一﹑字符串較長
string stmp
for(int i=0;i<10;i++)
{
stmp.replace(i.tostring()," ");
}
現在數字全部變為" "了﹐接著就是將非數字的字符串分開.
string[] stmp2=stmp.split(" ");
建立新的字串組﹐保存最后的結果()
arraylist stmp3;
for(int i=0;i<stmp2.lengh;i++)
{
if(stmp2[i]!="")
{
if(stmp.startwith(stmp2[i])==false)
{
stmp3.add(stmp.substring(0,stmp.indexof(stmp2[i]))
stmp=stmp.substring(stmp.indexof(stmp2[i]));
}
stmp3.add(stmp2[i])
stmp=stmp.substring(stmp2[i].length);
}
}二﹑如果字符串比較短,就沒有什么好說的了﹐直接判斷是否為數字就行了。將連續的數字保存在一個字串中就行了。
public string ExpDiv(ref string exp)
{
//分解EXP存入public static System.Collections.Queue save 调用一次出列一次
string optrStr = "",oprdStr = "";
for(int i = 0;i < exp.Length;i ++)
{
if(!this.IsOptr(exp[i].ToString()))//数字
{
exp = exp.Substring(i,exp.Length - i);
oprdStr += exp[i];
}
else if(this.IsOptr(exp[i].ToString()))//算符
{
exp = exp.Substring(i,exp.Length - i);//清除存储的数字
save.Enqueue(oprdStr);
oprdStr = "";
try
{
optrStr = exp[i].ToString();
}
catch(Exception err)
{
Console.WriteLine(err.Message);
}
save.Enqueue(optrStr);//算符只需一个字符
}
//i = 0;
}
return save.Dequeue().ToString();
}
/*楼上大哥的方法我试试先 呵呵 不知道满足我的要求没有 其实我只是简单地想把算术表达式分离成数字和运算符而已 没想到想了好久都不行呀*/
try {
Regex RegexObj = new Regex("\\d+|\\D+");
Match MatchResults = RegexObj.Match(SubjectString);
while (MatchResults.Success) {
MatchResults = MatchResults.NextMatch();
}
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
你的算法我实在不懂用(从来没用过Regex类呀还有Match)
foreach(Match m in Regex.Matches("123+456", @"\d+|\D+"))
{
q.Enqueue(m.Value);
}
Regex.Matches()的第二个参数没怎么看的懂
而且如果我要的算术表达式是"12*(3-5)-(55-3)"这样类似的带有括号的那么这招还管用吗?
而事实上要求也是这样的 :)