String type

In AskiaScript, a string variable contains characters (note that it may also be empty).

In this topic:

Defining a string variable

To create a string variable, assign a string value (or an empty string) between double quotes (").

You can include a double quote character in your string by escaping it using the slash character (). For example, to assign 'my text' within double quotes:
dim mystring = ""some text""
You can also include a carriage return character, to write a string on multiple lines.

The end of the string is the final non-escaped double quote character.

Examples
dim my_empty_string = ""
dim email  = "username@domain.com"
dim citation = "People says: "Bla bla bla""
dim multiline_string = "Hello world!
This is
a multi-line
string"

↑ Top of page ↑

Properties

Length

Returns the length of the string (number of characters)

Returns a Number.

dim my_string = "abcd" 
my_string.length  ' => 4
dim my_empty_string = "" 
my_empty_string.length ' => 0

↑ Top of page ↑

Methods

Warning: methods on string objects do not affect the current string. Instead, they return a new string. For example:

dim my_string = "CAPITAL"
my_string.ToLowerCase() ' => "capital"
my_string ' => "CAPITAL"

To affect the string, you can re-assign the new return value on it. as follows:

dim my_string = "CAPITAL"
my_string = my_string.ToLowerCase() ' => "capital"
my_string ' => "capital"

 

↑ Top of page ↑

Collate(text [, Comparison, Language])

Compares two strings using the local language.

Parameters
Examples
dim s = "abcde"
s.Collate("fghi") ' => negative value
dim s = "gfhi" 
s.Collate("abcde") ' => positive value
dim s = "abcde" 
s.Collate("abcde") ' => 0
dim s = "Fussballmanager" 
s.Collate("fußballmanager",1,"DEU") ' => 0 
dim s = "Fussballmanager" 
s.Collate("fußballmanager",2,1031) ' => 1 

↑ Top of page ↑

CollateAny(ArrayOfStrings [, Comparison, Language])

Compares a string against an array of strings, using the locale language.

Returns an array of numbers to indicate which strings have been matched.

Parameters
Examples
dim s = "abcde"
s.CollateAny({"ab"; "abcded"; "fghi"}) ' => returns {2}
dim s = "Fussballmanager"
s.CollateAny({"footbal" ; "fussballmanager"; "fußballmanager"},1,"DEU") ' => returns {2;3}

↑ Top of page ↑

DLdistance(text)

Calculates the distance between two strings using the Damerau-Levenshtein Distance. This distance is useful when finding spelling errors.

Returns a number.

Examples
dim s = "abcde" 
s.DLDistance("abce") ' => 1
dim s = "abcde" 
s.DLDistance("abdce") ' => 1
dim s = "abcde" 
s.DLDistance("abcde") ' => 0

↑ Top of page ↑

IndexOf(text [, caseSensitive])

Searches the text in the string and returns its position (based 1).

Parameters
Examples
dim s = "abcdefghijk"
s.IndexOf("cde") ' => 3
dim s = "abcdefghijk" 
s.IndexOf("BcD") ' => 2
dim s = "abcdefghijk" 
s.IndexOf("lmn") ' => DK (not found)
dim s = "abcdefghijk" 
s.IndexOf("CDE", true) ' => DK (not found with the case sensitive flag)

↑ Top of page ↑

IsDate([format])

Verifies that the string is formatted as a valid date, using:

Returns a Boolean.

Parameters
Examples
dim s = "22/03/2011" 
s.IsDate() ' => True
dim s = "22##03##2011" 
s.IsDate() ' => False
dim s = "22##03##2011" 
s.IsDate("dd##MM##yyyy") ' => True

 

↑ Top of page ↑

IsEmail()

Verifies that the string is formatted as a valid email address (x@x.xxx).

Returns a Boolean.

Examples
dim s = "askia@askia.com" 
s.IsEmail() ' => True
dim s = "askia#askia.com" 
s.IsEmail() ' => False

↑ Top of page ↑

IsMatch(regexp)

Verifies that the string matches the specified format, using a regular expression.

Returns a Boolean.

Parameters
Examples
dim email = "askia@askia.com" 
email.IsMatch("w+@[a-zA-Z_]+.[a-zA-Z]{2,3}$")

Regular expression to valid that a string contains the uk phone number pattern
+(##) ### ### #### or 00## ### ### ####

dim rgUkPhone = "(+|00)s*d{2}(s?d{3}){2}s?d{4}" 
'
("0044 207 689 5492").IsMatch(rgUkPhone) ' => true
("00442076895492").IsMatch(rgUkPhone) ' => true
("+44 207 689 5492").IsMatch(rgUkPhone) ' => true
("+442076895492").IsMatch(rgUkPhone) ' => true
("207 689 5492").IsMatch(rgUkPhone) ' => false
("44 207 689 5492").IsMatch(rgUkPhone) ' => false
("442076895492").IsMatch(rgUkPhone) ' => false
("Before it was 00 44 204 689 5492 and 
now it's 00 44 207 689 5492").IsMatch(rgUkPhone)
' => true
("My contact numbers are: 
+44 207 689 5492
or
0044 207 123 4567").IsMatch(rgUkPhone) ' => true

Same regular expression as above, but tests if the string only contains the UK phone number and nothing else.

dim rgOnlyUkPhone = "^(+|00)s*d{2}(s?d{3}){2}s?d{4}$" 
'
("+44 207 689 5492").IsMatch(rgOnlyUkPhone) ' => true
("0044 207 689 5492").IsMatch(rgOnlyUkPhone) ' => true
("Before it was 00 44 204 689 5492 
and now it's 00 44 207 689 5492").IsMatch(rgOnlyUkPhone)
' => false

↑ Top of page ↑

IsNumber()

Verifies that the string is formatted as a valid number.

Returns a Boolean.

Examples
dim s = "abcde" 
s.IsNumber() ' => 0
dim s = "123.99" 
s.IsNumber() ' => True
dim s = "123,99" 
s.IsNumber() ' => True
dim s = "123,999.32" 
s.IsNumber() ' => False thousand separators not supported

↑ Top of page ↑

IsUKPostCode()

Verifies that the string is formatted as a valid UK Postcode.

Returns a Boolean.

Examples
dim s = "L1 4AB" 
s.IsUKPostCode() ' => True
dim s = "L4B1C12" 
s.IsUKPostCode() ' => False

↑ Top of page ↑

Left(length)

Extracts part of the string, starting at the left-hand end.

Returns a String.

If the specified length is higher than the length of the string, it returns the entire string.

Parameters
Examples
dim s = "abcde" 
s.Left(3) ' => "abc"
dim s = "abcde" 
s.Left(10) ' => "abcde"

↑ Top of page ↑

QuickMatch(name)

Not yet implemented

Verifies that the string matches the specified format, using a regular expression from the library.

Parameters

Examples
dim s = "askia@askia.com" 
s.QuickMatch("IsEmail") ' => True 

↑ Top of page ↑

Replace(pattern, replacement[, caseSensitive])

Returns a new string and replaces the specified pattern with the specified replacement.

Returns a String.

Parameters
Examples
dim s = "Hello world!" 
s = s.Replace("Hello", "Hi") 
' => "Hi world!"
dim abc = "abc" 
abc = abc.Replace("A", "X") 
' => "Xbc"
dim case = "Sensitive" 
case.Replace("sensitive", "Insensitive", true) 
' => "Sensitive" Doesn't change
dim case = "Sensitive" 
case.Replace("sensitive", "Insensitive",false) 
' => "Insensitive"

↑ Top of page ↑

ReplaceRegexp(regexp, replacement[, modifier])

Available from version 5.5.2.

Returns a new string and replace the first match of the specified regexp pattern by the specified replacement. By default the text replacement is case sensitive. If there is 'i' (insensitive) modifier in the third parameter the text replacement is not case sensitive. If there is 'g' (global) modifier in the third parameter the replacement is done on all matches.

Returns a String.

Parameters
Examples
dim case = "Sensitive" 
case = case.ReplaceRegexp("s.*n", "Insen") 
' => "Sensitive" Doesn't change
dim s = "Sensitive" 
s = s.ReplaceRegexp("s.*n", "Insen", "i") 
' => "Insensitive"
dim case = "abcdef" 
case = case.ReplaceRegexp("[a-e]", "X") 
' => "Xbcdef" The replacement is done only on the first match
dim case = "abcdef" 
case = case.ReplaceRegexp("[a-e]", "X", "g") 
' => "XXXXXf" The replacement is done on all matches
dim case = "abcdef" 
case = case.ReplaceRegexp("[A-E]", "X", "g") 
' => "abcdef" Doesn't change (case sensitive)
dim case = "abcdef" 
case = case.ReplaceRegexp("[A-E]", "X", "gi") 
' => "XXXXXf" The replacement is done on all matches and case insensitive

↑ Top of page ↑

Right(length)

Extracts a section of the string, starting from the right-hand end.

Returns a Boolean.

If the specified length is higher than the length of the string, it returns the entire string.

Parameters

Examples
dim s = "abcde" 
s.Right(3) ' => "cde"
dim s = "abcde" 
s.Right(10) ' => "abcde"

↑ Top of page ↑

Split([separator])

Splits the string into an array of strings.

Returns a StringArray.

Parameters
Examples
dim s = "1,3,5,7,9" 
s.Split() ' => {"1"; "3"; "5"; "7"; "9"}
dim s = "a|b|c|d|e" 
s.Split("|") ' => {"a"; "b"; "c"; "d"; "e"}
dim s = "a|b|c|d|e" 
s.Split("|")[2] ' => "b" 

↑ Top of page ↑

SplitToNumbers([separator])

Available from version 5.3.3.

Splits the string into an array of numbers.

Returns a NumArray.

Parameters
Examples
dim s = "1,3,5,7,9" 
s.SplitToNumbers() ' => {1; 3; 5; 7; 9}
dim s = "1|3|5|7|9" 
s.SplitToNumbers("|") ' => {1; 3; 5; 7; 9}
dim s = "1|3|5|7|9" 
s.SplitToNumbers("|")[2] ' => 3
dim s = "1|3|toto|7|tata|12" 
s.SplitToNumbers("|") ' => {1; 3; DK; 7; DK; 12} 

↑ Top of page ↑

Substring(start [, length ])

Extracts a sub-string starting at the specified index (based 1), with a given length.

Returns a String.

Parameters
Examples
dim s = "abcde" 
s.Substring(2, 3) ' => "bcd"
dim s = "abcde" 
s.Substring(2) ' => "bcde"
dim s = "abcde" 
s.Substring(3, 10) ' => "cde"
dim s = "abcde" 
s.Substring(10, 2) ' => ""

↑ Top of page ↑

ToASCII()

Returns a new string in ASCII format. This is useful for converting UNICODE characters to ASCII.

Note: this version was introduced in version 5.3.5.3.

For example:

“234”.ToAscii()

returns "234".

↑ Top of page ↑

ToDate([format])

Converts the string to a date, using:

Returns DK when the string cannot be converted to a date.

Returns a Date.

Parameters
Examples
dim s = "22/03/2011" 
s.ToDate() ' => #22/03/2011#
dim t = "2011-03-22" 
t.ToDate("yyyy-MM-dd") ' => #22/03/2011#
dim invalidDate = "Hello world!" 
invalidDate.ToDate() ' => DK (Invalid date)

↑ Top of page ↑

ToHexa()

Available from version 5.3.3.

Attempts to convert the string, which represents a colour (hexa, rgb or rgba), to its Hexadecimal equivalent string.

Returns a String.

See also: String as color.

Examples
dim s = "#ff0000" ' Red color in Hexa
s.ToHexa() ' => "#ff0000" ' Remain the same
dim s = "0,255,0,0.5"  ' Green semi-transparent color in RGBA
s.ToHexa() ' => "#00ff00" ' Drop the alpha the green color 
' will be fully opaque
dim s = "0,0,255" ' Blue color in RGB
s.ToHexa() ' => "#0000ff"
dim s = "Not a color string" 
s.ToHexa() ' => "" ' fail to convert
dim s = "red" ' Named color is not interpreted
s.ToHexa() ' => ""

↑ Top of page ↑

ToLowerCase()

Returns a new string in lowercase.

Returns a String.

Example
dim s = "Hello World!" 
s = s.ToLowerCase() ' => "hello world!"

↑ Top of page ↑

ToNumber()

Converts the string to a number, if possible.

Returns a Number.

Returns DK when the string cannot be converted to number.

Warning: the thousands separator can trigger an error during the conversion. This is because in some countries (e.g. France), the comma is a decimal separator, while in other countries (e.g. the USA), the comma is a thousands separator. For example:

"123,456" is 123.456 for French systems but 123456 for American systems.

Therefore, to avoid confusion, the thousand separator is not permitted in the conversion.
Examples

dim s = "abcde"
s.ToNumber() ' => DK (not a valid number)
dim s = "123" 
s.ToNumber() ' => 123
dim s = "123.99" 
s.ToNumber() ' => 123.99
dim s = "123,456" 
s.ToNumber() ' => 123.456
dim s = "123,456.789" 
s.ToNumber() ' => DK (not a valid number)
' The thousand separator generate an exception

↑ Top of page ↑

ToRGB()

Available from version 5.3.3.

Attempts to convert a string representing a colour (hexa, rgb or rgba) to its RGB equivalent string.

Returns a String.

See also: String as color.

Examples
dim s = "#ff0000" ' Red color in Hexa
s.ToRGB() ' => "255,0,0"
dim s = "0,255,0,0.5" ' Green semi-transparent color in RGBA
s.ToRGB() ' => "0,255,0" ' Drop the alpha the green color 
' will be fully opaque
dim s = "0,0,255" ' Blue color in RGB
s.ToRGB() ' => "0,0,255" ' Remain the same
dim s = "Not a color string" 
s.ToRGB() ' => "" ' fail to convert
dim s = "red" ' Named color is not interpreted
s.ToRGB() ' => ""

↑ Top of page ↑

ToRGBA()

Available from version 5.3.3.

Attempts to convert the string, which represents a colour (hexa, rgb or rgba), to its RGBA equivalent string.

Returns a String.

See also: String as colour.

Examples
dim s = "#ff0000" ' Red color in Hexa
s.ToRGBA() ' => "255,0,0,1" ' Full opaque red color
dim s = "0,255,0,0.5" ' Green semi-transparent color in RGBA
s.ToRGBA() ' => "0,255,0,0.5" ' Remain the same
dim s = "0,0,255" ' Blue color in RGB
s.ToRGBA() ' => "0,0,255,1" ' Full opaque blue
dim s = "Not a color string" 
s.ToRGBA() ' => "" ' fail to convert
dim s = "red" ' Named color is not interpreted
s.ToRGBA() ' => ""

↑ Top of page ↑

ToUpperCase()

Returns a new string in uppercase.

Returns a String.

Example
dim s = "Hello world!" 
s = s.ToUpperCase() ' => "HELLO WORLD!"

↑ Top of page ↑

Trim()

Returns a new string without the trailing spaces before and after the string.

Returns a String.

Example
dim s = "  spaces before/after   " 
s = s.Trim() ' => "spaces before/after"

↑ Top of page ↑

ToString()

Returns an identical string.

Returns a String.

Example
dim s = "Hello world!" 
s.ToString() ' => "Hello world!"

↑ Top of page ↑

TypeOf()

Always returns "string".

Returns a String.

Example
dim s = "abc" 
s.TypeOf() ' => "string"

↑ Top of page ↑

Operators

Name Symbol / Expression Description Example
Equal =

Tests whether two strings are equal.

Returns a boolean.

To use this operator to perform a comparison, the condition should be between brackets, or else it will be considered an assignment operator
dim i = "MyText" 
(i = "MyText") ' => True
(i = "mytext") ' => False
Different <>

Tests whether two strings are not equal.

Returns a boolean.

dim i = "MyText" 
i <> "MyText" ' => False
i <> "mytext" ' => True
Concatenate +

Concatenates strings of characters.

Returns a string.

dim i = "MyFirstText" 
dim j = " is nice" 
i + j ' => MyFirstText is nice

↑ Top of page ↑

String as color

Available from version 5.3.3.

A string could represent a colour in RGB, RGBA or Hexa format.

↑ Top of page ↑

Create your own Knowledge Base