fromStringOr

Summary

Converts a string to its equivalent byte value or the provided default value if the conversion fails.

Usage

public static byte fromStringOr(string numberAsStr, byte defaultValue)

Returns

The byte equivalent of the string value or the provided default value if the string is not valid.

Parameters

numberAsStr

The string value to convert.

defaultValue

The default value to return if the conversion fails.

Description

Converts a string to its equivalent byte value or the provided default value if the conversion fails.

import System;

byte x = UInteger8.fromStringOr("100", 101);
Console.log(x); // 100 // conversion succeeded
byte y = UInteger8.fromStringOr("abc", 101);
Console.log(y); // 101 // conversion failed so default value returned

If the string being converted is numeric but does not fit into the byte data range [ 0, 255 ] (inclusive), the default value will be returned.

import System;

byte fits = UInteger8.fromStringOr("255", 100);
Console.log(fits); // 255
byte exceedByOne = UInteger8.fromStringOr("256", 100);
Console.log(exceedByOne); // 100
byte exceedByTwo = UInteger8.fromStringOr("257", 100);
Console.log(exceedByTwo); // 100
byte exceedByThree = UInteger8.fromStringOr("258", 100);
Console.log(exceedByThree); // 100

Examples

Basic Usage
1
2
3
4
5
6
import System;
 
byte valid = UInteger8.fromStringOr("100", 23);
Console.log(valid); // 100
byte invalid = UInteger8.fromStringOr("abc", 24);
Console.log(invalid); // 24

Share

HTML | BBCode | Direct Link