環形/課程/字串
外觀
< 環形
在本章中,我們將學習字串的建立和操作。
語法
cStr = "This is a string"
cStr2 = 'Another string'
cStr3 = :JustAnotherString
cStr4 = `Yet "another" 'string' ! `我們可以使用 len() 函式獲取字串長度(字串中的字母數量)。
語法
len(string) ---> string length示例
cStr = "How are you?"
see cStr + nl
see "String size : " + len(cStr) + nl語法
lower(string) ---> convert string letters to lower case
upper(string) ---> convert string letters to UPPER case示例
cStr = "Welcome To The Ring Programming Language"
see cStr + nl + upper(cStr) + nl + lower(cStr)我們可以透過字母索引訪問字串中的字母。
語法
string[index] ---> get string letter
string[index] = letter # set string letter示例
# print user name letter by letter (each letter in new line)
See "Hello, Enter your name : " give cName
for x = 1 to len(cName)
see nl + cName[x]
next
我們可以使用 for in 來獲取字串字母。
示例
# print user name letter by letter (each letter in new line)
See "Hello, Enter your name : " give cName
for x in cName
see nl + x
next我們可以修改字串字母。
示例
# convert the first letter to UPPER case
See "Enter your name : " give cName
cName[1] = upper(cName[1])
see "Hello " + cName我們可以使用 Left() 函式從字串中獲取指定數量的字元。
起始位置為 1。
語法
Left(string,count)示例
see left("Hello World!",5) # print Hello我們可以使用 Right() 函式從字串中獲取指定數量的字元。
起始位置為最右側的字元。
語法
Right(string,count)示例
see Right("Hello World!",6) # print World!我們可以使用 Trim() 函式從字串中刪除所有前導和尾隨空格。
語法
trim(string)示例
cMsg = " Welcome "
see trim(cMsg) # print Welcome我們可以使用 copy() 函式將字串複製多次。
語法
copy(string,count) ---> string replicqated count times示例
see copy("***hello***",3) # print ***hello******hello******hello***我們可以使用 Lines() 函式統計字串中行的數量。
語法
lines(string) ---> Number of lines inside the string示例
cStr = "Hello
How are you?
are you fine?"
see lines(cStr) # print 3我們可以使用 substr() 函式處理字串中的子字串。使用 Substr(),我們可以
- 查詢子字串
- 獲取從位置到末尾的子字串
- 獲取從位置開始的字元數量
- 將子字串轉換為另一個子字串
語法
substr(string,substring) ---> the starting position of substring in string示例
cStr = "Welcome to the Ring programming language"
see substr(cStr,"Ring") # print 16語法
substr(string,position) ---> Get substring starting from position to end示例
cStr = "Welcome to the Ring programming language"
nPos = substr(cStr,"Ring") # nPos = 16
see substr(cStr,nPos) # print Ring programming language語法
substr(string,position,count) ---> Get characters starting from position示例
cStr = "Welcome to the Ring programming language"
nPos = substr(cStr,"Ring") # nPos = 16
see substr(cStr,nPos,4) # print Ring語法
substr(string,substring,newsubstring) ---> Transformed string (Match case)
substr(string,substring,newsubstring,1) ---> Transformed string (Ignore case)示例
cStr = "Welcome to the New programming language"
see substr(cStr,"New","Ring") + nl # print Welcome to the Ring programming language
see substr(cStr,"new","Ring",1)+ nl # print Welcome to the Ring programming language我們可以使用 strcmp() 函式比較兩個字串。
語法
strcmp(cString1,cString2) ---> value = 0 if cString1 = cString2
value < 0 if cString1 < cString2
value > 0 if cString1 > cString2示例
see strcmp("hello","hello") + nl +
strcmp("bcd","abc") + nl輸出
0
-1
1我們可以使用 str2list() 函式將字串行轉換為列表項。我們還可以使用 list2str() 函式將列表轉換為字串。
語法
str2list(string) ---> list contains the string lines
list2str(list) ---> string contains the list items示例
/* output:
** Items : 4
** Item : Hello
** Item : How are you ?
** Item : are you fine ?
** Item : ok
** list2Str result = Hello
** How are you ?
** are you fine ?
** ok
** Done
*/
mystr = "Hello
How are you ?
are you fine ?
ok"
mylist = str2list(mystr)
see "Items : " + len(mylist) + nl
for x in mylist
see "Item : " + x + nl
next
newstr = list2str(mylist)
see "list2Str result = " + newstr
if mystr = newstr
see nl + "Done"
else
see nl + "Error!"
ok