Reputation: 19
I have an array of bytes that contains data. I would like to search for a specific string in the array of bytes. I dont know really how to do this in C#
byte[] ByteArray = File.ReadAllBytes(@"d:/MyDoc.docx");
String searchString = "Graphics";
How do I find the the word "Graphics" in the array dataArray? Thanks
Upvotes: 0
Views: 1022
Reputation: 415620
Docx files are zipped. The only strings you'll find there are relative to zip headers. There won't be any text from your document.
You can see this in Windows by changing the docx
file extension to zip
and then double-clicking the file. You'll find an archive with some XML
content, which can open with any Xml reader, or even notepad.
You could do the same thing manually in code (ie via System.IO.Compression
types), but you don't have to. There are other libraries that have already done much of the hard work for you to extract the archive and already know what files and schema to look for. Some of them freely available on NuGet.
Upvotes: 1
Reputation: 119
You can convert your array of bytes into string like here
string converted = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
and after that use the string.IndexOf()
or string.Contains()
method
Upvotes: 1