Easy way to delete text within string and keep numbers

Is there a easy way to delete text part in strings?

1 Like

One-liner assuming one number in each item:

dbl = double.Parse(string.Concat(str.Where(c => c == '-' || c == '.' || (c >= '0' && c <= '9'))));

str.gh (3.9 KB)

Faster version for multiple numbers:

str_multi.gh (5.2 KB)

private void RunScript(string str, ref object dbl)
  {
    dbl = ExtractDoublesFromString(str).ToArray();
  }

        public static IEnumerable<double> ExtractDoublesFromString(string str)
        {
            var len = str.Length;
            var curpos = 0;
            for (; ; )
            {
                double t = 0;
                curpos = FastStringToDouble(str, ref t, curpos);
                if (curpos != -1)
                {
                    yield return t;
                }
                else
                {
                    yield break;
                }

            }
        }

  public static double FirstDoubleFromString(string str)
  {
    double t = 0;
    FastStringToDouble(str, ref t);
    return t;
  }


  public static int FastStringToDouble(string str, ref double result, int start = 0)
  {
    var len = str.Length;
    if (start >= len) return -1;

    for (var i = start; i < len; i++)
    {
      var c = str[i];
      if (c >= '0' && c <= '9')
      {
        var neg = (i == start) ? false : (str[i - 1] == '-');

        var intResult = c - '0';

        var j = i + 1;
        for (; j < len; j++)
        {
          var curchar = str[j];

          if (curchar == '.')
          {
            result = intResult;

            var k = j + 1;
            var scale = 0.1;
            for (; k < len; k++)
            {
              var digitchar = str[k];
              if (digitchar < '0' || digitchar > '9')
                break;

              result += scale * (digitchar - '0');
              scale *= 0.1;
            }

            if (neg)
              result = -result;
            return k;

          }
          else
          {
            if (curchar < '0' || curchar > '9')
            {
              break;
            }

            intResult *= 10;
            intResult += curchar - '0';
          }
        }

        if (neg)
          intResult = -intResult;

        result = intResult;

        return j;


      }
      else if (c == '.' && i < len - 1)
      {
        var nc = str[i + 1];
        if (nc >= '0' && nc <= '9')
        {
          var neg = (i == start) ? false : (str[i - 1] == '-');
          result = (nc - '0');

          var scale = 0.1;
          var j = i + 2;

          for (; j < len; j++)
          {
            var curchar = str[j];
            if (curchar < '0' || curchar > '9')
            {
              break;
            }

            result += (curchar - '0') * scale;
            scale *= 0.1;
          }

          result *= 0.1;

          if (neg)
            result = -result;

          return j;
        }
      }
    }

    return -1;
  }