How to Use String and Container Types
1. String
String is a very useful data type. Almost every script people write involves some string manipulation. Luban offers a rich set of tools to do that easily. String type key word is string. The below example Luban code shows operations for string type.
1.1 Basic Set of Operators
Read/write with indexing:
s
= “Hello”; c = s[1]; // c is ‘e’
s
= “Hello”; s[0] = ‘h’; // s becomes “hello”
Data member iteration:
s
= “Hello”;
foreach( c in s
) std::console().write( c ); // will print out Hello
Arithmetic like operators
s
= “Hello”*2; //
result is “HelloHello”
s
= -“Hello”; // result is “olleH”
s
= “Hello” + “World”; // result “HelloWorld”
1.2 Member functions
//
The split() function that everyone wants
s
= “Hello World”;
words = s.split( ); //
words is a vector [“Hello”, “World”]
//
trim white space from front and back
s
= “ Hello”;
s.trimfront(); // s becomes “Hello”
s.
= “Hello \n\t”;
s.trimback();
// s become “Hello”
//search
s=”Hello”;
index=s.find(“Hell”); // index = 0
index2=s.find(“Hell”,2);
// index = null, faled to find Hell starting from
index 2l
index3=s.find(“Heaven”);
// index3 = null failed to find
truth=s.contains(“Hell”); // truth
= true
sub=s.substr(2,3);
// sub=”ll” from index 2 to index 3
//
content manipulation
s=”Hello”;
s.replace(“Hell”,
“Heaven”); // now s becomes “Heaveno”
s.lower();
// s becomes “heaveno”;
s.upper();
// s = “HEAVENO”
s.remove(6);
// s=”HEAVEN”, with 0 at index 6 removed
s.remove(“A”);
// s=”HEVEN”;
s.insert(1,
“A”); // s=”HEAVEN”;
s.clear();
// s=””
Vector type key word is vector.
x = 1; y=2; z=3; v = [x,y,z,4]; // v = [
1,2,3,4]
v[0] = 10; // v = [10,2,3,4]
total =
0; foreach(
element in v ) total += element; // total =
v.sort(); // v become
[2,3,4,10]
v2 = v+ [11,12,13]; // v2 become
[2,3,4,10,11,12,13]
v2.remove(0);
// v2 become [3,4,10,11,12,13]
v2.removeobj(10); // v2 become [3,4,11,12,13]
v3 = [
v2,v2]; // v3 is two dimensional vector
// push and pop
x=[1,2];
x.pushfirst(0); // x =[0,1,2]
x.pushlast(3); // x=[0,1,2,3]
y=x.popfirst(); // y=0, x=[1,2,3];
z=x.poplast(); // y=3, x=[1,2]
//sub vector
x=[0,1,2,3];
y=x.subvec(2,3); // y=[2,3]
.
Map type key word is map. Keys are unique in a map. Code examples:
x=1; y=2;
z = 3; word2number
= { “one” : x, “two” : y, “three” : z };
word2number[“four”]
= 4; // insert or replace
word2nubmer.remove(“one”);
// remove key “one” and associated 1
word2number.insert(“two”,2.0);
// no impact because key already exist
union =
word2number + { “five”:5, “six”:6};
foreach(key,val
in word2number ) std::console().writeline(key,’
‘,val); // iterate
emptymap = {:};
Set key word is set. Set is simply a collection of unique values. See examples:
oneset = { 1,
“hello”, 3.14 };
another = {
1,2};
union = oneset + another; //
union = { 1,2,”hello”,3.14}
minus = oneset – another ; // minus = { “hello”, 3.14}, common
values removed
joint = oneset – minus; // joint = {1}, the common values of oneset and another
foreach( val
in oneset ) std::console().writeline(val); // iterate
emptyset = {};
Casting operations among vector, set and map:
oneset = { 1,2,3}; onevec
= vector(oneset);
keyvec=[‘a’,’b’]; valvec = [‘A’,’B’]; onemap = map(keyvec, valvec);