Silk String
String
is surrounded by double quotation marks.
String assignment:
s="abc";
String format:
With the C like built-in function sprintf, we can format
integer, string and float by specifying %d, %s and %f.
sprintf supports the following specifier:
%c format character(string with
1 character), or the ASCII code of a character
%s format string
%d format integer
%o format octal number
%x format hexadecimal number
%X format hexadecimal(capital letters)
%f format float
s=sprintf("%d,%s,%f\r\n",100,"abc",100.23);
You can also use the following
Escape
characters in format:
\\
Backslash
\" Double quota
\a Beep
\b Backspace
\n New Line
\r Carriage Return
\v Vertical Tab
\t Horizontal Tab
String can be added with + operator directly:
s="I am "+" Hero";
There are 3 ways to check if the string is empty:
if(s=="") print("empty string");
if(s.size()==0) print("empty string");
if(!s) print("empty string");
Note: Empty string is false in logical operators, but empty string is
not equal to null, the output of the following code is empty,
not null.
s="";if(!s)
print("empty");if(s==null)
print("null");
Member functions of
String:
substr(index,len)
Return a substring according to the index of the first
character and length of the substring
s="Hello";s2=s.substr(1,2);//from the index(position) 1,return a new string with length 2, so s2="el";
find(str),find(str,index)
Find the first substring equal to str, for the second one, search
begins at index.
if str is found, return the index(position) of the first
character of the found substring, otherwise return -1.
s="Hello,world!";
pos=s.find("world");//pos=6, search begins at 0 by default
pos=s.find("world",10);//pos=-1, search begins at 10, not found
rfind(str),rfind(str,index)
Search backwards from the index to find
the substring equal to str
,
return the index(position) if str is found.
pos=s.rfind("world"); //pos=6, search backwards from the end of the string by default
pos=s.rfind("world",10); //pos=6,search backwards from 10, found and the index is 6
replace(oldStr,newStr)
Search the whole string to find all substrings equal to
oldStr, and replace them with newStr
s="Hello,world!";s=s.replace("Hello","Hi");//replace will return a new string, the original string remains unchanged
size()
return the size of the string(number of characters), you can also
use _len(s) to get the size
print(s.size());
split(seperateStr)
Split the string into an array, you can specify the separator
with seperateStr
s="abc,bbc,good";arrStr=s.split(",");
insert(pos,str)
insert string str at the position index
s="Good, how are you";s=s.insert(4," morning");
print(s);
erase(pos,len)
Remove the characters in the range [pos, pos+len), and return
a new string
s="Good, how are you";s=s.erase(4,2);
print(s);
trim()
Remove the leading and trailing whitespace,
carriage
return in the string, and return a new string
s=" Good, how are you ";s=s.trim();
print(s);
ltrim()
Remove the left side of leading and trailing whitespace,
carriage
return in the string, and return a new string
s=" Good, how are you ";s=s.ltrim();
print(s);
rtrim()
Remove the right side of leading and trailing whitespace,
carriage
return in the string, and return a new string
s=" Good, how are you ";s=s.rtrim();
print(s);
lower()
Return a string where all characters are in lower case.
s=" Good, how are you ";s=s.lower();
print(s);
upper()
Return a string where all characters are in upper case.
s=" Good, how are you ";s=s.upper();
print(s);