字串處理


無極 發表



字串處理跟字串函數的介紹

新網頁1

一般來說,使用者跟程式的溝通,大多是靠字串來傳遞資料

,例如:輸入帳號密碼....等等。所以說學會字串的處理是一

件很重要的事,而 VB 也提供了許多字串處理函式,我將大

概做一個介紹。

字串處理

● 字串的宣告

Dim title As String

title="VB"           則 title 這個變數的值就會被指定為VB

● 字串連接

title="Visual " & "Basic"        則 title 的值變為 Visual Basic

 

字串函數

●Asc(String) 傳回字串中第一個字元的字元值

    例:Asc("VB")  傳回 86

●Chr(charcode) 傳回代表某字元碼的字元,charcode為一代表

                            字元碼的整數

    例:Chr(86)  傳回字元 V

●Str(number)  將數值強制轉換成字串型態。傳回的字串前有

                         一空白字元,該字元保留為正負符號。

●Format    依指定格式將運算式轉成字串

    語法:Format(expression[,format[,firstdayofweek[,firstweekofyear]]])

                expression:有效的運算式

                format:格式說明

                firstdayofweek:代表一週中第一天的常數

                firstweekofyear:代表一年中第一週的常數

    日期及時間

    Format(#11/23/2004#,"yy/mm/dd")  傳回  04/11/23

    Format(#11/23/2004#,"yyyy,mmmm,dd  dddd")  傳回  2004,November,23 Tuesday

    Format(#11/23/2004#,"mmmm-dd")  傳回    November-23

    固定格式

格式 說明
General Number 顯示數值,但無千位數符號
Currency 顯示幣值格式
Fixed 於小數點前至少顯示 1位數,並於小數點後至少顯示 1位數
General Date 顯示日期或時間
Long Date 以系統之長日期格式顯示日期
Medium Date 以系統之中日期格式顯示日期
Short Date 以系統之短日期格式顯示日期
Long Time 以長時間格式顯示時間
Mediu Time 以12小時格式顯示小時、分及AM/PM
Short Time 以24小時格式顯示時間

    例:

    Format(100000,"Currency")    傳回  NT$100,000.00

    Format(#11/23/2004#,"Long Date")     傳回  2004年11月23日

    Format(#08:01:01#,"Long Time")     傳回  上午 08:01:01

    Format 的變化非常的多,在此我只大概介紹一下,其他的就

    靠各位自己去摸索了。

●LCase(string)  將字串轉為小寫

    Dim str1, str2 As String

    str1 = "Visual Basic 6.0"

    str2 = LCase(str1)    傳回  visual basic 6.0

●UCase(String)  將字串轉為大寫

    Dim str1, str2 As String

    str1 = "Visual Basic 6.0"

    str2 = UCase(str1)    傳回  VISUAL BASIC 6.0

●Left(string,lenght)    傳回字串左端起某長度的字串

    Dim str1, str2 As String

    str1 = "Visual Basic 6.0"

    str2 = Left(str1,6)    傳回  Visual 

●Right(string,length)    傳回字串又端起某長度的字串

    Dim str1, str2 As String

    str1 = "Visual Basic 6.0"

    str2 = Right(str1,9)    傳回  Basic 6.0

●Mid(string,start[,length])    傳回字串中從某位置起的某長度字串

    Dim str1, str2 As String

    str1 = "Visual Basic 6.0"

    str2 = Mid(str1,8,5)    傳回  Basic

●String(n,character)    傳回一個由 n個字元組成的字串

    Dim result As String

    result=String(4,"*")    傳回****

●StrReverse(string)    傳回一個字元順序倒置的字串

    Dim result As String

    result=StrReverse("Basic")    傳回cisaB

 

最後更新日期: 11/23/2004 4:54:42 PM