Zsktnm.TextScanner
0.1.2
dotnet add package Zsktnm.TextScanner --version 0.1.2
NuGet\Install-Package Zsktnm.TextScanner -Version 0.1.2
<PackageReference Include="Zsktnm.TextScanner" Version="0.1.2" />
<PackageVersion Include="Zsktnm.TextScanner" Version="0.1.2" />
<PackageReference Include="Zsktnm.TextScanner" />
paket add Zsktnm.TextScanner --version 0.1.2
#r "nuget: Zsktnm.TextScanner, 0.1.2"
#:package Zsktnm.TextScanner@0.1.2
#addin nuget:?package=Zsktnm.TextScanner&version=0.1.2
#tool nuget:?package=Zsktnm.TextScanner&version=0.1.2
TextScanner
Simple text scanner, built on IParsable<T>.
Inspired by Java Scanner
Install
You can obtain this package by Nuget
Usage
Create the scanner
Use the constructor with TextReader parameter or the static method FromConsole to create the Scanner.
// read from file
using (StreamReader reader = new("file.txt"))
{
Scanner scanner = new Scanner(reader);
// ...
}
// read from console
Scanner scanner = Scanner.FromConsole();
// ...
Read a 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 numbers: ");
while (!scanner.TryRead(out count))
{
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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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.