简体   繁体   中英

Remove tailing zero from comma separated string using c#

I need help on remove tailing 0 from comma separated string using c#

my data is string = "110, 0, 170, 0, 0"

output needed string = "110, 0, 170"

Help is appreciated,

Here you go, I chained multiple Linq Methods to achieve the goal.

using System; 
using System.Linq; 

var input = "110, 0, 170, 0, 0";
var splittedInput = input.Split(", ");
var resultArray = splittedInput.Reverse().SkipWhile(e => Convert.ToInt32(e) == 0).Reverse();
var result = string.Join(", ", resultArray); 

And you have the result in the result variable. I suggest you take a look at : SkipWhile , Reverse , and Convert.ToInt32()

Here is one way of doing it.

var input = "110, 0, 170, 0, 0";    
while (input.EndsWith(", 0"))
{
    input = input.Substring(0, input.Length - 3);
}
//input will now have the desired output 110, 0, 170

My 2 cents: Keep on searching from right to left until non zero item is found

void Main()
{
    
    string s = "110, 0, 170, 0, 0";
    //string s = "110, 0, 170";
    //string s = "110";
    Trimmer t = new Trimmer();
    var result = t.RTrimZero(s);
    Console.WriteLine(result);
}

public class Trimmer
{
    public string RTrimZero(string s)
    {
        bool IsEndingWithZero = false;
        Tuple<string, bool> result = null;
        do
        {
            result = CutIT(s);
            s = result.Item1;
        }
        while (result.Item2 == true);
        return s;
    }

    private Tuple<string, bool> CutIT(string s)
    {
        string collected = string.Empty;
        for (var x = s.Length - 1; x >= 0; x--)
        {
            char cur = s[x];
            if (cur.Equals(','))
            {
                //Console.WriteLine(collected);
                int val = Convert.ToInt32(collected);
                if (val == 0)
                {
                    //cut it
                    return new Tuple<string, bool>(s.Substring(0, x), true);
                }
                else
                {
                    return new Tuple<string, bool>(s, false);
                }
            }
            else
            {
                collected = s[x] + collected;
            }
        }
        return new Tuple<string, bool>(s, false);
    }
}

Similar to @Abdelkrim answer, but organized in a function

class Program
{
    static void Main(string[] args)
    {
        string input = "110, 0, 170, 0, 0";
        string output = RemoveTrailingZeros(input);

        Console.WriteLine(output);
    }

    static string RemoveTrailingZeros(string list)
    {
        int[] parts = list.Split(',').Select((s) => int.Parse(s.Trim())).ToArray();
        parts = parts.Reverse().SkipWhile((x) => x == 0).Reverse().ToArray();
        return string.Join(", ", parts);
    }
}

It converts the string to a list of integers, processes the list of integers using SkipWhile() from the end, and converts it back to a string.

This looks like part of a coding challenge which C# is not so well suited for, since the inputs and outputs are always given as strings in these challenges and C# is more data model-centric.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM