Parse without string split

前端 未结 2 1159
没有蜡笔的小新
没有蜡笔的小新 2021-01-05 08:10

This is a spin-off from the discussion in some other question.

Suppose I\'ve got to parse a huge number of very long strings. Each string contains a sequence of

2条回答
  •  礼貌的吻别
    2021-01-05 08:13

    There is no managed API to parse a double from a substring. My guess is that allocating the string will be insignificant compared to all the floating point operations in double.Parse.

    Anyway, you can save the allocation by creating a "buffer" string once of length 100 consisting of whitespace only. Then, for every string you want to parse, you copy the chars into this buffer string using unsafe code. You fill the buffer string with whitespace. And for parsing you can use NumberStyles.AllowTrailingWhite which will cause trailing whitespace to be ignored.

    Getting a pointer to string is actually a fully supported operation:

        string l_pos = new string(' ', 100); //don't write to a shared string!
        unsafe 
        {
            fixed (char* l_pSrc = l_pos)
            {               
                  // do some work
            }
        }
    

    C# has special syntax to bind a string to a char*.

提交回复
热议问题