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

compareTo(other as Lang.Object) as Lang.Number

Lexicographically compare self to some other string.

Parameters:

  • other(Lang.Object)

    The right hand side of a comparison.

Returns:

  • Lang.Number

    A negative value if self is less than other, zero if the objects are equivalent, and a positive value if self is greater than other.

Since:

API Level 5.0.0

Throws:

equals(other as Lang.Object or Null) as Lang.Boolean

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

Parameters:

  • other(Lang.Object)

    The Object to test against

Returns:

  • Lang.Boolean

    true if the Objects are equal, otherwise false

Since:

API Level 1.0.0

find(string as Lang.String) as Lang.Number or Null

Determine if the specified String exists in a String.

Parameters:

Example:

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

Returns:

  • Lang.Number

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

Since:

API Level 1.0.0

hashCode() as Lang.Number

Get a hash code value for a String.

Returns:

Since:

API Level 1.0.0

length() as Lang.Number

Get the number of characters in a String.

Returns:

Since:

API Level 1.0.0

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

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

Note:

Starting with 3.3.2, passing null for startIndex sets it to the start of the string and passing null for endIndex sets it to the end of the string. Passing in a negative number for either startIndex or endIndex offsets it from the end of the string.

Parameters:

  • startIndex(Lang.Number)

    Zero-based start index of the substring

  • endIndex(Lang.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:

  • Lang.String

    The substring of the String or null on error

Since:

API Level 1.0.0

toCharArray() as Lang.Array<Lang.Char>

Convert a String to an Array of Char objects.

Returns:

  • Lang.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 Lang.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:

  • Lang.Double

    A Double representation of the String

Since:

API Level 3.1.0

toFloat() as Lang.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:

  • Lang.Float

    A Float representation of the String

Since:

API Level 1.0.0

toLong() as Lang.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:

  • Lang.Long

    A Long representation of the String

Since:

API Level 3.1.0

toLongWithBase(base as Lang.Number) as Lang.Long or Null

Convert a String to a Long using a specified base.

Parameters:

  • base(Lang.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:

  • Lang.Long

    A Long representation of the String

Since:

API Level 3.1.0

toLower() as Lang.String

Convert a String to lowercase.

Returns:

Since:

API Level 1.0.0

toNumber() as Lang.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:

  • Lang.Number

    A Number representation of the String

Since:

API Level 1.0.0

toNumberWithBase(base as Lang.Number) as Lang.Number or Null

Convert a String to a Number using a specified base.

Parameters:

  • base(Lang.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:

  • Lang.Number

    A Number representation of the String

Since:

API Level 1.4.1

toString() as Lang.String

Convert a String to a String.

Returns:

Since:

API Level 1.0.0

toUpper() as Lang.String

Convert a String to uppercase.

Returns:

Since:

API Level 1.0.0

toUtf8Array() as Lang.Array<Lang.Number>

Convert a String to an Array of Number objects.

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

Returns:

  • Lang.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 Sep 23, 2024, 3:32:49 PM