UBinarySizeTryParse(ReadOnlySpanChar, BinarySizeOptions, NumberStyles, IFormatProvider, UBinarySize) Method

Tries to parse a span of characters into a UBinarySize structure.

Definition

Namespace: Ookii
Assembly: Ookii.BinarySize (in Ookii.BinarySize.dll) Version: 1.2.0+20f0bdf981c7baf115c68ccab612b1a031efcb4d
C#
public static bool TryParse(
	ReadOnlySpan<char> s,
	BinarySizeOptions options,
	NumberStyles style,
	IFormatProvider? provider,
	out UBinarySize result
)

Parameters

s  ReadOnlySpanChar
The span of characters to parse.
options  BinarySizeOptions
A bitwise combination of BinarySizeOptions values that indicates how multi-byte units in s are interpreted.
style  NumberStyles
A bitwise combination of NumberStyles values that indicates the style elements that can be present in s. A typical value is NumberStylesNumber.
provider  IFormatProvider
An object that provides culture-specific formatting information about s.
result  UBinarySize
When this method returns, contains the result of successfully parsing s, or an undefined value on failure.

Return Value

Boolean
if s was successfully parsed; otherwise, .

Remarks

The input must contain a number, followed by one of the following units: "B", "KB", "KiB", "MB", "MiB", "GB", "GiB", "TB", "TiB", "PB", "PiB", "EB", or "EiB". The "B" may be omitted, and any surrounding whitespace is ignored.

Depending on the value of options, the use of ulong units may also be allowed: "byte", "kilobyte", "kibibyte", "megabyte", "mebibyte", "gigabyte", "gibibyte", "terabyte", "tebibyte", "petabyte", "pebibyte", "exabyte", or "exbibyte". The "byte" suffix may be ommitted, and may also be the plural "bytes", regardless of whether the number is actually plural or not.

The units listed above are the default, invariant units based on the English language. You can parse localized units by using the BinaryUnitInfo class with the provider parameter, either directly or together with a CultureInfo object through the CultureInfoExtensionsWithBinaryUnitInfo(CultureInfo, BinaryUnitInfo) method.

The case of the units in s is ignored by default. Use the BinaryUnitInfoCompareOptions property to customize how units are matched.

The size of 1 KiB always equals 1024 bytes, and 1 MiB is 1,048,576 bytes, and so on.

By default, this method also treats 1 KB as 1,024 bytes, identical to 1 KiB, and 1 MB equals 1 MiB equals 1,048,576 bytes, and so on.

When options includes the BinarySizeOptionsUseIecStandard flag, the SI prefixes (those without an 'i') are treated as powers of ten, so that 1 kB equals 1,000 bytes, 1 MB == 1,000,000 bytes, and so on. The IEC prefixes are unchanged, and remain based on powers of two.

The value of options is not stored in the instance, and will therefore not affect the output when converting back to a string. Use an appropriate format string with the ToString(String, IFormatProvider) method if you wish to use decimal prefixes there as well.

Exceptions

ArgumentExceptionoptions is not valid.

See Also