Mega Code Archive

 
Categories / C# / Data Types
 

Reads count number of characters and returns them as a string with any null terminators removed

// --------------------------------------------------------------------------------------------------------------------- //   Copyright (c) SRT Solutions 2009. All rights reserved. // --------------------------------------------------------------------------------------------------------------------- namespace SRTSolutions.Elevate.IO {     using global::System;     using global::System.IO;     using global::System.Linq;     /// <summary>     /// Contains extension methods on the binary reader.     /// </summary>     public static class BinaryReaderExtensions     {         /// <summary>         /// Reads count number of characters and returns them as a string with any         /// null terminators removed.         /// </summary>         /// <param name="reader">The reader.</param>         /// <param name="count">The count.</param>         /// <returns>the characters read from the file, as a string</returns>         public static string ReadString(this BinaryReader reader, int count)         {             if (reader == null)                 throw new ArgumentNullException("reader", "reader is null.");             var charsWithoutNulls =                 reader.ReadChars(count)                 .TakeWhile(character => character != '\0')                 .ToArray();             return new string(charsWithoutNulls);         }     } }