Melang

Logo

A script language of preemptive scheduling coroutine in single thread

View the Project on GitHub Water-Melon/Melang

String

Import

str = Import('str');
bin2hex

Binary bytes string to hex string.

str.bin2hex(bin);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');
rc = Import('rc');

sys.print(str.bin2hex(rc.rc4('HI', 'key')));

Output:

4325
hex2bin

Hex string to binary bytes.

str.hex2bin(hex);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.hex2bin('4849'));

Output:

HI
bin2int

Binary bytes to an integer.

str.bin2int(bin);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.bin2int('HI'));

Output:

18505
int2bin

Integer to binary bytes.

str.int2bin(i);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.int2bin(18505));

Output:

HI
bin2real

Binary bytes to real number.

str.bin2real(bin);

Input:

Return value:

Example:

sys.print(str.bin2real('HI'));

Output:

1070435769529469910793714477087121352287059968.000000
real2bin

Real number to binary bytes.

str.real2bin(r);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.real2bin(1070435769529469910793714477087121352287059968.000000));

Output:

HI
b2s

Copy binary bytes into a string.

str.b2s(bin);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.b2s(100));

Output:

d
s2b

String to a given type value.

str.s2b(s, type);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.s2b('d', 'int'));

Output:

100
strlen

Calculate string length.

str.strlen(s);

Input:

Return value:

Example:

sys.print(str.strlen('this is a test'));

Output:

14
split

Return part of a string.

str.split(s, offset, len);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.split('this is a test', -10));

Output:

 is a test
slice

Split a string by a set of characters.

str.slice(s, seps);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.slice('this is a test', ' s'));

Output:

[thi, i, a, te, t, ]
kmp

Find the first occurrence of a string by KMP algorithm.

kmp(s, pattern);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.kmp('this is a test', 'is'));

Output:

2
strstr

Find the first occurrence of a string

str.strstr(s, pattern);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.strstr('this is a test', 'is'));

Output:

2
strncmp

String comparison of the first n characters.

str.strncmp(s1, s2, n);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.strncmp('this is a test', 'is', 2));

Output:

false
strcmp

String comparison.

str.strcmp(s1, s2);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.strcmp('drink', 'drink'));

Output:

true
strseqcmp

String comparison.

str.strseqcmp(s1, s2);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.strseqcmp('drinking', 'drink'));

Output:

1
reg_equal

Match string by a regular expression.

str.reg_equal(exp, text);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.reg_equal('.*', 'test'));
sys.print(str.reg_equal('.*ed', 'test'));

Output:

true
false
reg_match

Match string by a regular expression and get matched string pieces.

str.reg_match(exp, text);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.reg_match('((this )*i(s)).*', 'this is a reg exp test.'));

Output:

[this , s, this is, this is a reg exp test., ]
replace

Replace all occurrences of the search string with the replacement string.

str.replace(&dict, &str);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.replace(['aaa':'123', 'bbb': 'Hello'], 'aaabbbcccaaa'));

Output:

123Helloccc123
trim

Strip whitespace (or other characters) from the beginning and end of a string.

str.trim(s, mask);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.trim(" hello \t\n"));

Output:

hello
upper

Make character uppercase.

str.upper(s);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.upper('hello'));

Output:

HELLO
lower

Make character lowercase.

str.lower(s);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

str.print(sys.lower('HELLO'));

Output:

hello
join

Join all elements in an array into a string, using a string as separator.

str.join(glue, arr);

Input:

Return value:

Example:

str = Import('str');
sys = Import('sys');

sys.print(str.join(',', [1, [1,2], 'asd']));

Output:

1,Array,asd