string.eh

String operations. More...

use "string.eh"

Functions

def ba2utf(ba: [Byte]): String;
def ca2str(ca: [Char]): 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.chars(): [Char];
def String.cmp(str: String): Int;
def String.concat(str: String): String;
def String.endswith(suffix: String): Bool;
def String.find(sub: String): Int;
def String.format(args: [Any]): String;
def String.hash(): Int;
def String.indexof(ch: Char): Int;
def String.lcase(): String;
def String.lindexof(ch: Char): Int;
def String.replace(oldch: Char, newch: Char): String;
def String.split(ch: Char): [String];
def String.startswith(prefix: String, ofs: Int = 0): Bool;
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(): [Byte];

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 [Char].

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 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.indexof(ch: Char): 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: Char): 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.startswith(prefix: String, ofs: Int = 0): Bool;
Tests if this string starts with the specified prefix. If offset is given, prefix string is tested at the corresponding position of this string.

def String.endswith(suffix: String): Bool;
Tests if the string ends with the specified suffix.

def String.replace(oldch: Char, newch: Char): String;
Returns new string where all occurences of the old character are replaced by new character.

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.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: Char): [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: [Any]): 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(): [Char];
Returns characters of this string as character array.

def String.utfbytes(): [Byte];
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: [Char]): String;
Creates new string with characters given from specified character array.

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