This topic tells you how to specify and manipulate string data in scripts.
The following examples illustrate correct string literals in DGScript:
str1 = 'Single quotes'; str2 = "Double quotes"; str3 = 'Double quotes in "single" quotes'; str4 = "Single quotes in 'double' quotes"; str5 = "escape quote "";
In strings, you can use a backslash () to escape the following reserved characters:
For example, the following code sets the string block's value to C::
@parent.string.value = "C:";
Function | Description and Example |
---|---|
String(obj) |
Takes an input object, casts the object as a string, and returns the string. The following example returns 123 as a string. String(123); |
String(num,16) |
Takes an input base-10 number, converts the number to base 16, and returns the base-16 number as a string. The following example returns the string 1b. String(27,16); |
str.length |
Counts the number of characters in an input string and returns the number. The following example returns the number 4. "acbd".length; |
str.charAt(pos) |
Returns the character at the specified position in an input string. The first position is represented as 0. The following example returns the character b. "abc".charAt(1); |
str.charCodeAt(pos) |
Returns the character code for the character at the specified position in an input string. The first position is represented as 0. The following example returns 98, the character code for the letter "b". "abc".charCodeAt(1); |
str.indexOf(searchStr) |
Returns the position of the first character of the first occurrence of the search string, or returns -1 if the search string is not found. The first position is represented as 0. The following example returns the number 1. "abc".indexOf("bc"); |
str.lastIndexOf(searchStr) |
Returns the position of the first character of the last occurrence of the search string, or returns -1 if the search string is not found. The first position is represented as 0. The following example returns the number 2. "abb".lastIndexOf("b"); |
str.split(delimiter) |
Takes an input string that contains delimiters and returns an array of strings. The following example returns the array ["1","2","3"]. "1,2,3".split(","); |
str=list.join(char) |
Takes an input list and converts it to an output string by joining items in the list with a provided delimiting character. The following example returns the string 1/2/3. [1,2,3].join("/"); |
str.subStr(start, length) |
Extracts the specified number of characters starting from the specified position, and returns the substring. The first position is represented as 0. The following example returns the string bc. "abcd".subStr(1,2); |
str.subString(start,end) |
Extracts the substring beginning with the specified start position and ending one position before the specified end value. The first position in a string is represented as 0. The following example returns the string b. "abcd".subString(1,2); |
str.replace(regexPattern, replace) |
Replaces all instances of the specified regular expression pattern with the replacement string. The following example returns the string d d. "DabcG DdefG".replace(/D(w+)/g, "d"); |
str.replaceAll(find, replace) |
Replaces all instances of the specified find string with the specified replace string. The following example returns the string 1_2_3. "1#2#3".replaceAll("#","_"); |
str.toLowerCase() |
Converts all letters in a string to lowercase letters. The following example returns the string dg. "DG".toLowerCase(); |
str.toUpperCase() |
Converts all letters in a string to uppercase letters. The following example returns the string DG. "dg".toUpperCase(); |
str.encodeUriComponent() |
Encodes an input URI component. The following example returns the string a%20b. "a b".encodeUriComponent(); |
str.decodeUriComponent() |
Decodes an encoded URI component string. The following example returns the string a b. "a%20b".decodeUriComponent(); |
str.splitQuery() |
Splits the input query string and decodes the string into an Object. The following example returns the Object {"a b": "1", "c": "d"}. "a%20b=1&c=d".splitQuery(); |
str.encodeBase64(); |
Encodes the input string, using base-64 encoding. The following example returns an encoded string. "abc123".encodeBase64(); |
str.decodeBase64(); |
Decodes a string that was encoded using str.encodeBase64(). The following example returns a decoded string. "YWJjMTQ0".decodeBase64(); |
str.md5(); |
Generates a 16-byte hash value that identifies the input string. The following example returns a 16-byte hash value. "abc123".md5(); |
str.sha1(); |
Generates a 20-byte hash value that identifies the input string. The following example returns a 20-byte hash value. "abc123".sha1(); |
str.sha256(); |
Generates an almost-unique 256-bit hash value that identifies the input string. The following example returns a 256-byte hash value. "abc123".sha256(); |
You can specify a number format using the following subset of ICU formatting patterns:
Formatting Pattern | Description |
---|---|
Zero (0) | Represents a single digit and is included even if the digit is a non-significant zero. |
Number sign (#) | Represents a single digit and is omitted if the digit is a non-significant zero. |
Decimal (.) | Represents the decimal separator. |
Hyphen (-) | Represents a minus sign. |
Comma (,) | Represents the grouping separator. |
The letter E | Separates a number and that number's exponent. |
Plus sign (+) | When used before an exponent, indicates to prefix a positive exponent with a plus sign. |
Percent symbol (%) | When used in a prefix or suffix, indicates to multiply the number by 100 and show the number as a percentage. |
Per mile sign (‰ or u2030) | Indicates to multiply the number by 1000 and show the number as per mile. |
Currency sign (¤ or u00A4) | Gets replaced by the currency name. |
Single quotes (') | Used to quote special characters. |
Semicolon (;) | Separates the positive and negative patterns if both are present. |
val = "123"; if (0+val == val) return "It's Numeric!";