fromStringOr

Summary

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

Usage

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

Returns

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

import System;

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

import System;

unsigned int fits = UInteger32.fromStringOr("4294967295", 100);
Console.log(fits); // 4294967295
unsigned int exceedByOne = UInteger32.fromStringOr("4294967296", 100);
Console.log(exceedByOne); // 100
unsigned int exceedByTwo = UInteger32.fromStringOr("4294967297", 100);
Console.log(exceedByTwo); // 100
unsigned int exceedByThree = UInteger32.fromStringOr("4294967298", 100);
Console.log(exceedByThree); // 100

Examples

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

Share

HTML | BBCode | Direct Link