跳轉到內容

環/教程/檢查資料型別和轉換

來自華夏公益教科書
<

檢查資料型別和轉換

[編輯 | 編輯原始碼]

在本章中,我們將學習有關可用於以下目的的函式:

  • 檢查資料型別
  • 檢查字元
  • 轉換



檢查資料型別

[編輯 | 編輯原始碼]

以下函式可用於檢查資料型別

  • isstring()
  • isnumber()
  • islist()
  • type()
  • isnull()



IsString() 函式

[編輯 | 編輯原始碼]

使用 IsString() 函式,我們可以知道值是否為字串。

語法

	IsString(value) ---> 1 if the value is a string or 0 if not

示例

	see isstring(5) + nl +		# print 0
	    isstring("hello") + nl	# print 1



IsNumber() 函式

[編輯 | 編輯原始碼]

使用 IsNumber() 函式,我們可以知道值是否為數字。

語法

	IsNumber(value) ---> 1 if the value is a number or 0 if not

示例

	see isnumber(5) + nl +		# print 1
	    isnumber("hello") + nl	# print 0



IsList() 函式

[編輯 | 編輯原始碼]

使用 IsList() 函式,我們可以知道值是否為列表。

語法

	IsList(value) ---> 1 if the value is a list or 0 if not

示例

	see islist(5) + nl +		# print 0
	    islist("hello") + nl +	# print 0
	    islist([1,3,5]) 		# print 1

pair: 資料型別; Type()

Type() 函式

[編輯 | 編輯原始碼]

我們可以使用 Type() 函式來知道值的型別。


語法

	Type(value) ---> The Type as String

示例

	    see Type(5) + nl +		# print NUMBER
	    Type("hello") + nl +	# print STRING
	    Type([1,3,5]) 		# print LIST



IsNULL() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsNULL() 函式來檢查值是否為 null。

語法

	IsNULL(value) ---> 1 if the value is NULL or 0 if not

示例

	    see isnull(5) + nl +	# print 0
	    isnull("hello") + nl +	# print 0
	    isnull([1,3,5]) + nl +	# print 0
	    isnull("") + nl +		# print 1
	    isnull("NULL")		# print 1



檢查字元

[編輯 | 編輯原始碼]

以下函式可用於檢查字元

  • isalnum()
  • isalpha()
  • iscntrl()
  • isdigit()
  • isgraph()
  • islower()
  • isprint()
  • ispunct()
  • isspace()
  • isupper()
  • isxdigit()

IsAlNum() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsAlNum() 函式來測試字元或字串。

語法

	IsAlNum(value) ---> 1 if the value is digit/letter or 0 if not

示例

	see isalnum("Hello") + nl +	# print 1
	    isalnum("123456") + nl +	# print 1
	    isalnum("ABCabc123") + nl +	# print 1
	    isalnum("How are you")  	# print 0 because of spaces

IsAlpha() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsAlpha() 函式來測試字元或字串。

語法

	IsAlpha(value) ---> 1 if the value is a letter or 0 if not

示例

	see isalpha("Hello") + nl +	# print 1
	    isalpha("123456") + nl +	# print 0
	    isalpha("ABCabc123") + nl +	# print 0
	    isalpha("How are you")  	# print 0

IsCntrl() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsCntrl() 函式來測試字元或字串。

語法

	IsCntrl(value) ---> 1 if the value is a control character (no printing position) or 0 if not

示例

	See iscntrl("hello") + nl +	# print 0 
	    iscntrl(nl)			# print 1

IsDigit() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsDigit() 函式來測試字元或字串。

語法

	IsDigit(value) ---> 1 if the value is a digit or 0 if not

示例

	see isdigit("0123456789") + nl +	# print 1
	    isdigit("0123a") 			# print 0

IsGraph() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsGraph() 函式來測試字元或字串。

語法

	IsGraph(value) ---> 1 if the value can be printed (Except space) or 0 if not

示例

	see isgraph("abcdef") + nl +	# print 1
	    isgraph("abc def") 		# print 0


IsLower() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsLower() 函式來測試字元或字串。

語法

	IsLower(value) ---> 1 if the value is lowercase letter or 0 if not

示例

	see islower("abcDEF") + nl + 	# print 0
	    islower("ghi")		# print 1


IsPrint() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsPrint() 函式來測試字元或字串。

語法

	IsPrint(value) ---> 1 if the value occupies a printing position or 0 if not

示例

	see isprint("Hello") + nl + 		# print 1
	    isprint("Nice to see you") + nl +   # print 1
	    isprint(nl)				# print 0


IsPunct() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsPunct() 函式來測試字元或字串。

語法

	IsPunct(value) ---> 1 if the value is a punctuation character or 0 if not

示例

	see ispunct("hello") + nl +	# print 0
	    ispunct(",") 		# print 1


IsSpace() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsSpace() 函式來測試字元或字串。

語法

	IsSpace(value) ---> 1 if the value is a white-space or 0 if not

示例

	see isspace("   ") + nl +	# print 1
	    isspace("test") 		# print 0


IsUpper() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsUpper() 函式來測試字元或字串。

語法

	IsUpper(value) ---> 1 if the value is an uppercase alphabetic letter or 0 if not

示例

	see isupper("welcome") + nl +	 # print 0 
	    isupper("WELCOME") 		 # print 1


IsXdigit() 函式

[編輯 | 編輯原始碼]

我們可以使用 IsXdigit() 函式來測試字元或字串。

語法

	IsXdigit(value) ---> 1 if the value is a hexdecimal digit character or 0 if not

示例

	see isxdigit("0123456789abcdef") + nl +  # print 1
	    isxdigit("123z") 			 # print 0



以下函式可用於轉換

  • number()
  • string()
  • ascii()
  • char()
  • hex()
  • dec()
  • str2hex()
  • hex2str()

Number() 函式

[編輯 | 編輯原始碼]

我們可以使用 Number() 函式或 + 運算子將字串轉換為數字。

語法

	Number(string) ---> Number
	0 + string ---> Number

示例

	see number("5") + 5 + nl 	# print 10
	see 0 + "10" + 2		# print 12

String() 函式

[編輯 | 編輯原始碼]

我們可以使用 String() 函式或 + 運算子將數字轉換為字串。

語法

	String(number) ---> String
	"" + number ---> String

示例

	see string(5) + 5 + nl 		# print 55
	see "" + 10 + 2			# print 102


Ascii() 函式

[編輯 | 編輯原始碼]

我們可以使用 Ascii() 函式獲取字母的 ASCII 碼。

語法

	Ascii(character) ---> ASCII Code

示例

	See ascii("m") + nl + 	# print 109
	    ascii("M") 		# print 77

Char() 函式

[編輯 | 編輯原始碼]

我們可以使用 Char() 函式將 ASCII 碼轉換為字元。

語法

	Char(ASCII Code) ---> character

示例

	See char(109) + nl + 	# print m
	    char(77) 		# print M


Hex() 函式

[編輯 | 編輯原始碼]

我們可以使用 Hex() 函式將十進位制轉換為十六進位制。

語法

	Hex(decimal) ---> hexadecimal

示例

	See hex(10) + nl + 	# print a
	    hex(200)		# print c8

Dec() 函式

[編輯 | 編輯原始碼]

我們可以使用 Dec() 函式將十六進位制轉換為十進位制。

語法

	Dec(hexadecimal) ---> decimal

示例

	See dec("a") + nl + 	# print 10
	    dec("c8")		# print 200

Str2hex() 函式

[編輯 | 編輯原始碼]

我們可以使用 Str2hex() 函式將字串字元轉換為十六進位制字元。

語法

	Str2hex(string) ---> hexadecimal string

示例

	See str2hex("hello")  	# print 68656c6c6f


Hex2str() 函式

[編輯 | 編輯原始碼]

我們可以使用 Hex2str() 函式將十六進位制字元轉換為字串。

語法

	Hex2Str(Hexadecimal string) ---> string

示例

	See hex2str("68656c6c6f")  	# print hello


華夏公益教科書