Zsktnm.TextScanner
0.1.0
See the version list below for details.
dotnet add package Zsktnm.TextScanner --version 0.1.0
NuGet\Install-Package Zsktnm.TextScanner -Version 0.1.0
<PackageReference Include="Zsktnm.TextScanner" Version="0.1.0" />
paket add Zsktnm.TextScanner --version 0.1.0
#r "nuget: Zsktnm.TextScanner, 0.1.0"
// Install Zsktnm.TextScanner as a Cake Addin
#addin nuget:?package=Zsktnm.TextScanner&version=0.1.0
// Install Zsktnm.TextScanner as a Cake Tool
#tool nuget:?package=Zsktnm.TextScanner&version=0.1.0
TextScanner
Simple text scanner, built on IParsable<T>
.
Inspired by Java Scanner
Usage
Create the scanner
You can use constructor with TextReader
parameter or static method FromConsole
.
// read from file
using (StreamReader reader = new("file.txt"))
{
Scanner scanner = new Scanner(reader);
// ...
}
// read from console
Scanner scanner = Scanner.FromConsole();
// ...
Read Single Value
If you want to parse your value, you can use Read<T>
or TryRead<T>
methods, where T
must be IParseble<T>
.
Console.WriteLine("Enter the count of numbers: ");
int count = scanner.Read<int>();
Console.WriteLine($"Enter the count of dates: ");
while(true)
{
if (scanner.TryRead(out count))
{
break;
}
Console.WriteLine("Invalid input. Try again");
}
You can also use the ReadBlock
, ReadChar
, ReadLine
and ReadToEnd
methods, if you don't want to parse your input.
ReadBlock
method returns the first value bounded by spaces. Note, that if input contains only whitespaces ReadBlock
returns string.Empty
, and if we have empty input (typicaly, the end of stream) it returns null
.
Read Multiple Values
Use ReadValues<T>
method to enumerate values:
Console.WriteLine("Enter the values: ");
int[] values = scanner.ReadValues<int>().Take(count).ToArray();
It is possible to skip every uncorrect input by using the parameter:
foreach (DateTime dateTime in scanner.ReadValues<DateTime>(skipOnErrors: true).Take(count))
{
Console.WriteLine($"Accepted {dateTime}");
}
Or you can change wrong inputs by default value (-1
in example below):
Console.WriteLine("Enter the values: ");
int[] values = scanner.ReadValues(defaultValue: -1).Take(count).ToArray();
You also can enumerate strings by ReadBlocks
and ReadLines
methods.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.