fromStringOr

Summary

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

Usage

public static unsigned short fromStringOr(string numberAsStr, unsigned short defaultValue)

Returns

The unsigned short 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 unsigned short value or the provided default value if the conversion fails.

import System;

unsigned short x = UInteger16.fromStringOr("100", 101);
Console.log(x); // 100 // conversion succeeded
unsigned short y = UInteger16.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 unsigned short data range [ 0, 65535 ] (inclusive), the default value will be returned.

import System;

unsigned short fits = UInteger16.fromStringOr("65535", 100);
Console.log(fits); // 65535
unsigned short exceedByOne = UInteger16.fromStringOr("65536", 100);
Console.log(exceedByOne); // 100
unsigned short exceedByTwo = UInteger16.fromStringOr("65537", 100);
Console.log(exceedByTwo); // 100
unsigned short exceedByThree = UInteger16.fromStringOr("65538", 100);
Console.log(exceedByThree); // 100

Examples

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

Share

HTML | BBCode | Direct Link