Read until extension.

This commit is contained in:
Alexandru Macocian
2021-04-01 14:43:18 +02:00
parent 8d1355ecbc
commit ee341af2c3
2 changed files with 28 additions and 1 deletions
@@ -1,4 +1,5 @@
using System.IO;
using System.Text;
namespace System.Extensions
{
@@ -36,5 +37,31 @@ namespace System.Extensions
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
public static string ReadUntil(this StreamReader sr, string delim)
{
var sb = new StringBuilder();
bool found = false;
while (!found && !sr.EndOfStream)
{
for (int i = 0; i < delim.Length; i++)
{
char c = (char)sr.Read();
sb.Append(c);
if (c != delim[i])
break;
if (i == delim.Length - 1)
{
sb.Remove(sb.Length - delim.Length, delim.Length);
found = true;
}
}
}
return sb.ToString();
}
}
}