fromStringOr

Summary

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

Usage

public static short fromStringOr(string numberAsStr, short defaultValue)

Returns

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

import System;

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

import System;

short fits = Integer16.fromStringOr("32767", 100);
Console.log(fits); // 32767
short exceedByOne = Integer16.fromStringOr("32768", 1);
Console.log(exceedByOne); // 1
short exceedByTwo = Integer16.fromStringOr("32769", 1);
Console.log(exceedByTwo); // 1
short exceedByThree = Integer16.fromStringOr("32770", 1);
Console.log(exceedByThree); // 1

Examples

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

Share

HTML | BBCode | Direct Link