String

A string may contain a sequence of characters. Strings can be defined in the code using double quotes:

var str = "hello world"
// or even multiline!
var multistr = "hello
world
and once
more!" 

However, currently strings cannot contain any special characters (like \n, etc), but it is a priority issue and is to be implemented.

Operations

Strings can be subject to the following operations:
  • Concatenation via the + sign
  • Repetition by multiplying a string by an integer

Methods

The string type defines the following methods:
  • bool to_b: converts to boolean (empty string = false, else true)
  • int to_i: converts to integer (returns 0 on error)
  • float to_f: converts to float (returns 0.0 on error)
  • bool is_i: checks if the string contains a valid integer representation
  • bool is_f: checks if the string contains a valid float representation
  • bool has(string str): checks if the string contains a substring
  • bool starts_with(string str): checks if the string starts with a substring
  • bool ends_with(string str): checks if the string ends with a substring
  • string trim: trims off the unpritable characters at the ends of the string
  • string substr(int from, int len=0): returns a substring from the position with given length (or till the end of the source string)
  • int find(string str, int from=0): gets the offset of the leftmost occurence of a substring in string, starting the search at given offset (or -1 if not found)
  • int find_last(string str, int from=0): gets the offset of the rightmost occurence of a substring in string, starting the search at given offset from right (or -1 if not found)
  • string replace(string from, string to): replaces a substring with another substring
  • string insert(int at, string str): inserts a string into given position
  • string reverse: reverses the string
  • int size: gets string length
  • string[] split(string token, int max=0): splits the string into an array of substrings by a token, allowing only max items in the array (0 = any number)
  • string join(string[] array): joins an array of strings together, using the current string as glue
  • int ord: gets the character code of the first character
  • string at(int pos): gets a character at given position

The type also defines several static methods:
  • string NL: returns "\n"
  • string ENDL: returns "\r\n"
  • string TAB: returns "\t"
  • string QUOTE: returns "\""

Last edited May 6, 2011 at 7:47 PM by impworks, version 3

Comments

No comments yet.