跳轉到內容

環形/課程/字串

來自華夏公益教科書,開放的書籍,為開放的世界

在本章中,我們將學習字串的建立和操作。

字串字面量

[編輯 | 編輯原始碼]

語法

	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() 函式

[編輯 | 編輯原始碼]

我們可以使用 Left() 函式從字串中獲取指定數量的字元。

起始位置為 1。

語法

	Left(string,count)

示例

	see left("Hello World!",5) # print Hello

Right() 函式

[編輯 | 編輯原始碼]

我們可以使用 Right() 函式從字串中獲取指定數量的字元。

起始位置為最右側的字元。

語法

	Right(string,count)

示例

	see Right("Hello World!",6) # print World!

Trim() 函式

[編輯 | 編輯原始碼]

我們可以使用 Trim() 函式從字串中刪除所有前導和尾隨空格。

語法

	trim(string)

示例

	cMsg = "     Welcome      "
	see trim(cMsg)			# print Welcome

Copy() 函式

[編輯 | 編輯原始碼]

我們可以使用 copy() 函式將字串複製多次。

語法

	copy(string,count) ---> string replicqated count times

示例

	see copy("***hello***",3) # print ***hello******hello******hello***

Lines() 函式

[編輯 | 編輯原始碼]

我們可以使用 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(),我們可以

  • 查詢子字串
  • 獲取從位置到末尾的子字串
  • 獲取從位置開始的字元數量
  • 將子字串轉換為另一個子字串

查詢子字串

[編輯 | 編輯原始碼]

語法

	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() 函式比較兩個字串。

語法

	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() 函式將字串行轉換為列表項。我們還可以使用 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


華夏公益教科書