none
I want to cut the string from the example above using the split and trim functions. Can't we make it in one line? RRS feed

  • Question

  • ex)12456,test      ,45,45,4563

    I want to use c # to cut to the second digit of [,] in the example above and then remove the whitespace with the trim function.


    -> 12456,test

    Monday, September 2, 2019 10:29 AM

All replies

  • You can do something like :

    string sString = "12456,test      ,45,45,4563";
    string sResult = sString.Substring(0, sString.IndexOf(',', sString.IndexOf(',') + 1)).TrimEnd();

    Monday, September 2, 2019 11:19 AM
  • or use LINQ and String.Join():

    var result = String.Join(",", "12456,test      ,45,45,4563".Split(',').Select(i => i.Trim()).Take(2));

    Monday, September 2, 2019 11:38 AM