Class: Toybox.Lang.String

Inherits:
Toybox.Lang.Object show all

Overview

String objects represent a sequence of characters, and provide methods for string operations.

Since:

API Level 1.0.0

Instance Method Summary collapse

Instance Method Details

equals(object) as Boolean

Test if an Object instance is equal to another instance of an Object.

Parameters:

  • object(Object)

    The Object to test against

Returns:

  • Boolean

    true if the Objects are equal, otherwise false

Since:

API Level 1.0.0

find(string as String) as Number or Null

Determine if the specified String exists in a String.

Parameters:

  • string(String)

    String to find

Example:

var myString = "Go bananas with Monkey C!";
var index = myString.find("bananas"); // index is 3

Returns:

  • Number

    The index of the start of the specified String, or null if not found

Since:

API Level 1.0.0

hashCode() as Number

Get a hash code value for a String.

Returns:

  • Number

    The hash code for the String

Since:

API Level 1.0.0

length() as Number

Get the number of characters in a String.

Returns:

  • Number

    The length of the String

Since:

API Level 1.0.0

substring(startIndex as Number, endIndex as Number) as String or Null

Create a new String that contains the contents of the current String from a start position to an end position.

Parameters:

  • startIndex(Number)

    Zero-based start index of the substring

  • endIndex(Number)

    End position of the substring, exclusive

Example:

var myString = "Go bananas with Monkey C!";
var mySubString = myString.subString(3, 10); // mySubString is "bananas"

Returns:

  • String

    The substring of the String or null on error

Since:

API Level 1.0.0

toCharArray() as Array<Char>

Convert a String to an Array of Char objects.

Returns:

  • Array

    A Char Array representation of the String, where each character in the String is an element in the Array

Since:

API Level 1.3.0

toDouble() as Double or Null

Convert a String to a Double.

If a String is in the numeric form of "123" or "123.45", convert it to a Double. Additional characters after the detected floating point value will be ignored. Strings that cannot be interpreted as a Double, or whose value exceeds that which can be represented in a Double, will result in a null value.

Example:

var myString;
var myNum;

myString = "123";
myNum = myString.toDouble(); // myNum is 123.000000

myString = "3.14"
myNum = myString.toDouble(); // myNum is 3.140000

myString = "192.168.0.1"
myNum = myString.toDouble(); // myNum is 192.167999

myString = "Hello There!"
myNum = myString.toDouble(); // null

Returns:

  • Double

    A Double representation of the String

Since:

API Level 3.1.0

toFloat() as Float or Null

Convert a String to a Float.

If a String is in the numeric form of "123" or "123.45", convert it to a Float. Additional characters after the detected floating point value will be ignored. Strings that cannot be interpreted as a Float, or whose value exceeds that which can be represented in a Float, will result in a null value.

Example:

var myString;
var myNum;

myString = "123";
myNum = myString.toFloat(); // myNum is 123.000000

myString = "3.14"
myNum = myString.toFloat(); // myNum is 3.140000

myString = "192.168.0.1"
myNum = myString.toFloat(); // myNum is 192.167999

myString = "Hello There!"
myNum = myString.toFloat(); // null

Returns:

  • Float

    A Float representation of the String

Since:

API Level 1.0.0

toLong() as Long or Null

Convert a String to a Long.

If a String is in the numeric form of "123", it can be converted to a Long. Additional characters after the detected number value will be ignored. Strings that cannot be interpreted as a Long, or whose value exceeds that which can be represented in a Long, will result in a null value.

Example:

var myString;
var myNum;

myString = "123";
myNum = myString.toLong(); // myNum is 123

myString = "3.14"
myNum = myString.toLong(); // myNum is 3

myString = "1200 E. 151st. Street"
myNum = myString.toLong(); // myNum is 1200

myString = "Hello There!"
myNum = myString.toLong(); // null

Returns:

  • Long

    A Long representation of the String

Since:

API Level 3.1.0

toLongWithBase(base as Number) as Long or Null

Convert a String to a Long using a specified base.

Parameters:

  • base(Object)

    The base of the input string. If the value of base is zero, the string content is expected to have syntax similar to that of integer constants, which includes:

    • An optional sign character ('+' or '-')

    • An optional prefix for octal or hexadecimal ('0' or '0x')

    • A sequence of digits in the prefixed base, or decimal if none was specified. If the base value is between 2 and 36, the format expected for the number is valid digits and/or letters that represent integers of the specified radix (from '0' to 'z' or 'Z' for base 36).

Example:

var myString;
var myNum;

myString = "10";
myNum = myString.toLongWithBase(2);    // myNum is 2

myString = "FF";
myNum = myString.toLongWithBase(16);   // myNum is 255
myNum = myString.toLongWithBase(0x10); // myNum is 255

Returns:

  • Long

    A Long representation of the String

Since:

API Level 1.4.1

toLower() as String

Convert a String to lowercase.

Returns:

  • String

    A new lowercase String

Since:

API Level 1.0.0

toNumber() as Number or Null

Convert a String to a Number.

If a String is in the numeric form of "123", it can be converted to a Number. Additional characters after the detected number value will be ignored. Strings that cannot be interpreted as a Number, or whose value exceeds that which can be represented in a Number, will result in a null value.

Example:

var myString;
var myNum;

myString = "123";
myNum = myString.toNumber(); // myNum is 123

myString = "3.14"
myNum = myString.toNumber(); // myNum is 3

myString = "1200 E. 151st. Street"
myNum = myString.toNumber(); // myNum is 1200

myString = "Hello There!"
myNum = myString.toNumber(); // null

Returns:

  • Number

    A Number representation of the String

Since:

API Level 1.0.0

toNumberWithBase(base as Number) as Number or Null

Convert a String to a Number using a specified base.

Parameters:

  • base(Object)

    The base of the input string. If the value of base is zero, the string content expected to have syntax similar to that of integer constants, which includes:

    • An optional sign character ('+' or '-')

    • An optional prefix for octal or hexadecimal ('0' or '0x')

    • A sequence of digits in the prefixed base, or decimal if none was specified. If the base value is between 2 and 36, the format expected for the number is valid digits and/or letters that represent integers of the specified radix (from '0' to 'z' or 'Z' for base 36).

Example:

var myString;
var myNum;

myString = "10";
myNum = myString.toNumberWithBase(2);    // myNum is 2

myString = "FF";
myNum = myString.toNumberWithBase(16);   // myNum is 255
myNum = myString.toNumberWithBase(0x10); // myNum is 255

Returns:

  • Number

    A Number representation of the String

Since:

API Level 1.4.1

toString() as String

Convert a String to a String.

Returns:

  • String

    A String representation of String

Since:

API Level 1.0.0

toUpper() as String

Convert a String to uppercase.

Returns:

  • String

    A new uppercase String

Since:

API Level 1.0.0

toUtf8Array() as Array<Number>

Convert a String to an Array of Number objects.

Each Number represents one byte of the UTF-8 representation of the String.

Returns:

  • Array

    An Array representation of the String, where each byte in the string is an element in the Array

Since:

API Level 1.3.0


Generated Apr 22, 2021 10:10:32 AM