string.eh

String operations. More...

use "string.eh"

Functions

def Any.tostr(): String;
def ba2utf(ba: BArray): String;
def ca2str(ca: CArray): String;
def chstr(ch: Int): String;
def Int.tobase(base: Int): String;
def Int.tobin(): String;
def Int.tohex(): String;
def Int.tooct(): String;
def Long.tobase(base: Int): String;
def String.ch(at: Int): Int;
def String.chars(): CArray;
def String.cmp(str: String): Int;
def String.concat(str: String): String;
def String.find(sub: String): Int;
def String.format(args: Array): String;
def String.hash(): Int;
def String.indexof(ch: Int): Int;
def String.lcase(): String;
def String.len(): Int;
def String.lindexof(ch: Int): Int;
def String.split(ch: Int): [String];
def String.substr(from: Int, to: Int): String;
def String.todouble(): Double;
def String.tofloat(): Float;
def String.toint(): Int;
def String.tointbase(base: Int): Int;
def String.tolong(): Long;
def String.tolongbase(base: Int): Long;
def String.trim(): String;
def String.ucase(): String;
def String.utfbytes(): BArray;

Description

String is a sequence of unicode characters. Every character is a number in range 0..65535. Strings are immutable, once created string cannot change. If you need changeable strings, use either StrBuf or CArray.

Starting from release 2.0, you can use square brackets [] to get individual character or substring:

var str = "abcdef"
// Use [at] to get character
var ch = str[2]
// Use [from:to] to get substring
println(str[1:3])  /* bc */
// If first argument omitted, then it is beginning of the string
println(str[:4])   /* abcd */
// If last argument omitted, then it is end of the string
println(str[3:])   /* def */

Function details

def Any.tostr(): String;
Returns string representation of the value. If value is: It is safe to call this function for values of other types but returned values are platform dependant and you should not rely on them.

def Int.tobin(): String;
Returns string representing this Int as an unsigned binary number.

def Int.tooct(): String;
Returns string representing this Int as an unsigned octal number.

def Int.tohex(): String;
Returns string representing this Int as an unsigned hexadecimal number.

def Int.tobase(base: Int): String;
Returns string representing this Int as a signed number in the given base. Argument base should be in range 2..36.

def Long.tobase(base: Int): String;
Returns string representing this Long as a signed number in the given base. Argument base should be in range 2..36.

def String.toint(): Int;
Parses this string as a signed decimal integer. If string is not representing valid number, then null is returned.

def String.tointbase(base: Int): Int;
Parses this string as a signed integer in the given base. Argument @emph base should be in range 2..36.

def String.tolong(): Long;
Parses this string as a signed decimal long integer. If string is not representing valid number, then null is returned.

def String.tolongbase(base: Int): Long;
Parses this string as a signed long integer in the given base. Argument base should be in range 2..36.

def String.tofloat(): Float;
Parses this string as a single precision floating point number. If string is not representing valid number, then null is returned.

def String.todouble(): Double;
Parses this string as a double precision floating point number. If string is not representing valid number, then null is returned.

def String.len(): Int;
Returns length of the string.

def String.ch(at: Int): Int;
Returns character at given position in the string. You may also use square brackets, as with array.

def String.indexof(ch: Int): Int;
Returns index of the first occurence of given character in the string. If string does not contain given character then -1 is returned.

def String.lindexof(ch: Int): Int;
Returns index of the last occurence of given character in the string. If string does not contain given character then -1 is returned.

def String.find(sub: String): Int;
Returns index of the first occurence of given substring in the string. If string does not contain given substring then -1 is returned.

def String.substr(from: Int, to: Int): String;
Returns substring of the string. Returned substring contains all characters in range from..to-1.

def String.ucase(): String;
Returns string with all characters converted to the upper case.

def String.lcase(): String;
Returns string with all characters converted to the lower case.

def String.concat(str: String): String;
Concatenates this string with another string. The same is done by operator '+'.

def String.cmp(str: String): Int;
Compares this string with another string lexicographically. Returns zero if two strings are equal, value less than zero if this string is less than argument and value greater than zero if this string is greater than argument.

def String.trim(): String;
Removes all whitespaces from the beginning and the end of the stream. This includes removing of ' ', '\t', '\r' and '\n'.

def String.split(ch: Int): [String];
Splits the string around given character. Examples:
"foo,and,baaz".split(',')  // gives array ["foo", "and", "baaz"]
"foo,and,baaz".split('a')  // gives array ["foo,", "nd,b", "", "z"]

def String.format(args: Array): String;
Returns formatted string using this string as format specification and given arguments. Format specifiers are substrings of the form %n where n is from 0 to 9. Each format specifier is substituted with corresponding value from array args. Specifier %% is substituted with percent character. For example:
"The price of %0 has decreased by %1%%".format(["cookies", 8])
// gives "The price of cookies has decreased by 8%"

def String.chars(): CArray;
Returns characters of this string as character array.

def String.utfbytes(): BArray;
Encodes this string in modified UTF-8 format and returns it as byte array.

def String.hash(): Int;
Computes 32-bit hash of this string using default Java hashing algorithm.

def ca2str(ca: CArray): String;
Creates new string with characters given from specified character array.

def ba2utf(ba: BArray): String;
Decodes byte sequence as string in modified UTF-8 format.

def chstr(ch: Int): String;
Returns string containing single character.