English 中文(简体)
如何从具有特定起始字符串的内存流中删除数据
原标题:How to remove data from a memory stream with a specific starting string

在将数据写入word文档模板之前,我必须从Memorystream中删除数据,该数据从一个特定的字符串开始,一直到流的末尾。

我尝试过以下操作,但当我将流转换为字符串时,它会给出一些编码的随机代码。在转换后的字符串中找不到特定的字符串。

        Memorystream ms;
        StreamReader rdr = new StreamReader(ms);
        string st = rdr.ReadToEnd();
        if (st.IndexOf("EmployeeDetail") != -1)
        {
            int start = st.IndexOf("EmployeeDetail");
            string toWrite = st.Substring(0, start);
        }

        var stream = new MemoryStream();
        var writer = new StreamWriter(stream);
        writer.Write(st);
        writer.Flush();
        stream.Position = 0;
        return stream;
问题回答

这似乎正是您所需要的:

void Main()
{
    var encoding = System.Text.Encoding.Default;

    var inputStream = new MemoryStream();
    inputStream.Write(encoding.GetBytes("Hello, World!"));

    var outputStream = CreateWithReplace(inputStream, ", World", encoding);
    var outputString = encoding.GetString(outputStream.ToArray());
    
    Console.WriteLine(outputString);
}

private MemoryStream CreateWithReplace(MemoryStream inputStream, string textToRemove, System.Text.Encoding encoding)
{
    var outputStream = new MemoryStream();
    inputStream.Position = 0;
    using (StreamReader reader = new StreamReader(inputStream, encoding))
    {
        string inputText = reader.ReadToEnd();
        int start = inputText.IndexOf(textToRemove);
        if (start != -1)
        {
            string outputText = inputText.Substring(0, start);
            using (var writer = new StreamWriter(outputStream, encoding))
            {
                writer.Write(outputText);
                writer.Flush();
            }
        }
        else
        {
            inputStream.WriteTo(outputStream);
        }
    }
    return outputStream;
}

它将Hello输出到控制台





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签