Monday, April 20, 2009

Split String so simple and easy [vb split string function]

แยกข้อมูลแบบสตริงอย่างง่ายๆ

1. Format of Raw Data for split
รูปแบบของข้อมูลที่จะนำมาแยกสตริง
You should know about data format for your spliting, such as
คุณควรจะทราบรูปแบบของข้อมูลที่คุณจะแยก เช่น
1.1 what delimiter ? space(” “) or slash(”/”) or mix format
อักขระที่ใช้แยกคืออะไร ใช้ ช่องว่าง ” ” หรือ สแลส “/” หรือแบบผสม
1.2 what data type to convert
จะเปลี่ยนข้อมูลที่แยกได้ไปเป็นรูปแบบใด
2. Function to use
ฟังก์ชั่นที่ใช้
Use Split Function for split data
ใช้ฟังก์ชั่น Split
Sample Code ตัวอย่างการเขียนโปรแกรม
creat one textbox and one button
สร้าง ออบเจก textbox และ Commandbutton



Option Explicit Private Sub CommandButton1_Click()
’error handler for Do Loop action over one step
On Error GoTo Err
Dim RawData As String

Dim SplitData() As String

Dim i, IndexData As Integer

Dim Answer As Variant

IndexData = 1

RawData = TextBox1.Text

SplitData = Split(RawData)

‘find number of data array

Do Until IndexData = 0

If IsNull(SplitData(IndexData - 1)) = False Then

IndexData = IndexData + 1

Else: IndexData = 0

End If

Loop

Err:

For i = 0 To IndexData - 2 Step 1
’subtract for IndexData startpoint at 1 and overstep Do Loop 1

MsgBox SplitData(i)

Next i

End Sub

No comments:

Post a Comment