Zsktnm.TextScanner 0.1.2

dotnet add package Zsktnm.TextScanner --version 0.1.2
NuGet\Install-Package Zsktnm.TextScanner -Version 0.1.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Zsktnm.TextScanner" Version="0.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Zsktnm.TextScanner --version 0.1.2
#r "nuget: Zsktnm.TextScanner, 0.1.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Zsktnm.TextScanner as a Cake Addin
#addin nuget:?package=Zsktnm.TextScanner&version=0.1.2

// Install Zsktnm.TextScanner as a Cake Tool
#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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
0.1.2 121 9/22/2023
0.1.1 108 9/19/2023
0.1.0 119 9/15/2023