将C ++ istringstream用法转换为C#

时间:2016-02-13 18:26:31

标签: c# c++

我正在重写一些代码并想知道如何将其从C ++转换为C#:

istringstream vertexString = new istringstream(s);

//number of vertices
int numVertices;
vertexString>>numVertices;

1 个答案:

答案 0 :(得分:1)

将其添加到静态字符串实用程序类:

public static Stream ToStream(this string str)
{
    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(str);
    writer.Flush();
    stream.Position = 0;
    return stream;
}

像这样使用。 s必须是字符串。

int numVertices;
using (var stringStream = s.ToStream())
{
    stringStream>>numVertices;
}