如何使用c#从“Rs1500 / ONLY”中删除“/ ONLY”

时间:2013-10-25 04:50:17

标签: c# replace

string oldstring = textBox9.Text;
string newstring = oldstring.Remove(0, 2);
string o = newstring.Remove(4, 7);

现在,我想只获得"1500",其余的东西要删除。

我该怎么做?请帮帮我。

4 个答案:

答案 0 :(得分:1)

您可以使用Replace

string oldstring = textBox9.Text;
string newstring = oldstring.Replace("Rs","").Replace("/ONLY","");

这应该只给你1500

答案 1 :(得分:1)

尝试以下代码

string newstring = Regex.Replace(oldstring, @"[^\d]", "");

它应该有用。

答案 2 :(得分:1)

有很多方法可以做到这一点..您可以使用拆分(),如下所示:

 string oldstring = "Rs1500/ONLY";
 string[] newstring = oldstring.Split('/');
 string o = newstring[0];

这将为您提供“RS 1500”。 如果你想删除RS,那么只需在上面的代码中添加以下代码:

 string final = newstring[0].ToString().Replace("Rs","");

那是全部

答案 3 :(得分:0)

试试这种方式

    string ss = "Rs1500/ONLY";
    string[] newss = ss.Split('/');
    ss = newss[0].ToString().Replace("Rs","");//Or try string.Empty() for instead of "" 

通过输出

查看此demo code :

enter image description here