【问题标题】:remove all characters after X character删除 X 字符后的所有字符
【发布时间】:2018-05-31 01:30:07
【问题描述】:

请检查变量“mystr”的值,其中两部分数字之间有“-”号。我想找到“-”然后删除所有字符,然后我想找到相同的“-”并删除从第一个到直到那个的所有字符。我知道这很简单,但由于我是新手,所以在 c# 上没有得到准确的解决方案。

public void test()
    {
        string mystr = "1.30-50.50";

        //first output I want is-  "1.30" 
        //second output I want is-  "50.50" 
    }

【问题讨论】:

标签: c#


【解决方案1】:

使用string.Split方法:

var mystr = "1.30-50.50";
var result = mystr.Split('-');
var a = result[0]; //"1.30" 
var b = result[1]; //"50.50" 

【讨论】:

    【解决方案2】:

    你也可以String.IndexOf方法

    string mystr = "1.30-50.50";
    int indexOfDash = mystr.IndexOf('-');
    string firsResult = mystr.Substring(0, indexOfDash);
    string secondResult = mystr.Substring(indexOfDash + 1, mystr.Length - indexOfDash - 1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 2020-10-03
      • 2012-03-08
      • 2019-09-21
      相关资源
      最近更新 更多