2020年2月25日 星期二

VB字串 常用處理函數

Len:計算字串的長度。
範例:strLen=Len("Taiwan")
結果:6


Mid:依開始位置擷取固定長度字串。
範例:strLen=Mid("Taiwan",1,2)
結果:ai


Left:將字串由左側擷取固定長度。
範例:strLen=Left("Taiwan",2)
結果:Ta
 

Right:將字串由右側擷取固定長度。
範例:str=Right(" Taiwan ",2)
結果:an


Lcase:將字串中的大寫字母轉成小寫。
範例:str=Lcase("Taiwan")
結果:taiwan


Ucase:將字串中的小寫字母轉成大寫。
範例:str=Ucase("Taiwan")
結果:TAIWAN


Trim:刪頭尾空白
範例:str=Trim("   Taiwan    ")
結果:Taiwan


Ltrim:刪字串左側的空白
範例:str=Ltrim("  Taiwan    ")
結果:Taiwan   


Rtrim:刪字串右側的空白
範例:str=Rtrim("  Taiwan    ")
結果:  Taiwan


Replace:將字串中的某字串,以另一字串代替。
範例:str=Replace("Taiwan","Tai"," Nan ")
結果:Nanwan


Instr:傳回子字串在字串中的第一次出現位置。
範例:strLen=Instr("ABCabc","a")
結果:1


Space:產生指定個數空白。
範例:Space(10)
結果:10個空白


String:產生指定個數的字元。
範例:str=String(5,"$")
結果:$$$$$


範例:str=String(4,66)
結果:BBBB
 
StrReverse:將字串前後反轉。
範例:str=StrReverse(" Taiwan ")
結果:nawiaT


Split:字串分割
範例:str=Split("Taiwan","T")
結果:
str(0)="T"
str(1)="aiwan"


StrComp:字串比較
範例:
str1=StrComp("Taiwan","Tai")
str2=StrComp("Taiwan","wan")
str3=StrComp("Taiwan","Taiwan")
結果:
str1=1
str2=-1
str3=0

沒有留言:

張貼留言