环形/课程/字符串
外观
< 环形
在本章中,我们将学习字符串的创建和操作。
语法
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