Advertisement
Guest User

Untitled

a guest
Mar 30th, 2021
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. public string ParsePercent(string expression)
  2. {
  3.     var list = SplitAndKeep(expression, expression.Where(x => x != '%' && !char.IsDigit(x)).ToArray()).ToList();
  4.  
  5.     if (list.Count > 0 && list[0].Contains('%'))
  6.     {
  7.         list[0] = list[0].Replace("%", "");
  8.     }
  9.  
  10.     for (int i = 1; i < list.Count; i++)
  11.     {
  12.         if (!char.IsDigit(list[i][0]))
  13.             continue;
  14.  
  15.         if (list[i].Contains('%'))
  16.         {
  17.             list[i] = (int.Parse(list[i - 2]) * int.Parse(list[i].Replace("%", "")) / 100).ToString();
  18.         }
  19.     }
  20.     return string.Join("",list);
  21. }
  22.  
  23. public static IEnumerable<string> SplitAndKeep(string s, char[] delims)
  24. {
  25.     int start = 0, index;
  26.  
  27.     while ((index = s.IndexOfAny(delims, start)) != -1)
  28.     {
  29.         if (index - start > 0)
  30.             yield return s.Substring(start, index - start);
  31.         yield return s.Substring(index, 1);
  32.         start = index + 1;
  33.     }
  34.  
  35.     if (start < s.Length)
  36.     {
  37.         yield return s.Substring(start);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement