9/04/2013


การตัดข้อความ การแยกประโยคด้วยคำหรือตัวอักษร

1. Split() การตัดข้อความ


Syntax
Split(ByVal Expression As String, Optional ByVal Delimiter As String)


Example

Dim test_date As String = "16 มิถุนายน 2556"
Dim getStr() As String = Split(test_date, " ")

For i = 0 To getStr.Length - 1
        
         MsgBox(getStr(i))
         ' 1. 16
         ' 2.  มิถุนาย
         ' 3.  2556

Next

** บทความที่เกี่ยวข้อง **
ฟังก์ชัน Mid ตัดคำใน vb.net

Posted on Wednesday, September 04, 2013 by nuyingnaja

การสลับคำจาก ตัวแรกให้ไปอยู่ตัวสุดท้าย และตัวสุดท้ายมาอยู่ตัวแรก ด้วย ฟังก์ชัน StrReverse

1. StrReverse() สลับคำ กลับคำ


Syntax
InStrRev(ByVal StringCheck As String, ByVal StringMatch As String, Optional ByVal Start As Integer)

Example

Dim test_word As String
test_word = "I Love Thailand"
 

' need  dnaliahT evoL I

Dim word As Integer =  StrReverse(test_word)   'dnaliahT evoL I

จากฟังก์ชันนี้จะเห็นได้ว่า ตัวอักษรจะสลับตำแหน่งกัน จากหน้ามือเป็นหลังมือ
 

Posted on Wednesday, September 04, 2013 by nuyingnaja

Public Function InStr(ByVal String1 As String, ByVal String2 As String, Optional ByVal Compare As Microsoft.VisualBasic.CompareMethod = Binary) As Integer
     Member of Microsoft.VisualBasic.Strings
Summary:

การแทนที่คำ ด้วย ฟังก์ชัน Replace

1. InStr() ค้นหาคำที่ต้องการ


Syntax
InStr(ByVal String1 As String, ByVal String2 As String)
InStrRev(ByVal String1 As String, ByVal String2 As String)

Example

Dim test_word As String
test_word = "I Love Thailand"
 

' need "h"

Dim location As Integer = InStr(test_word, "h")          ' 9
Dim location2 As Integer = InStrRev(test_word, "h")   ' 9
 

Posted on Wednesday, September 04, 2013 by nuyingnaja





การตัดคำที่มีประโยคยาว แต่ต้องการเฉพาะคำที่ต้องการเท่านั้น โดยใช้ ฟังก์ชัน Mid ตัดคำ ตัวอย่างดังนี้
 

Syntax
Mid(ByVal str As String, ByVal Start As Integer, ByVal Length As Integer)


Example

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim test_word As String
        test_word = "Thailand Only"

        ' need "Thailand"
        Dim A, B As String
        A = Mid(test_word, 1, 8)                     ' Thailand
        B = Microsoft.VisualBasic.Left(test_word, 8) ' Thailand

        ' need "Only"
        Dim C, D As String
        C = Mid(test_word, 10, 4)                     ' Only
        D = Microsoft.VisualBasic.Right(test_word, 4) ' Only


        test_word = "I Love Thailand"

        ' need "Love"
        Dim F, G As String
        F = Mid(test_word, 3, 4)                     ' Love

        ' ใช้ ฟังก์ชันค้นหาคำ ตำแหน่งของคำนั้นก่อน ตัวอย่างนี้ค้นหา "Love" หรือ "L" ก่อนก็ได้
        Dim locationStart As Integer = InStr(test_word, "Love")
        Dim locationStop As Integer = "Love".Length
        G = Mid(test_word, locationStart, locationStop) ' Love
End Sub

** บทความที่เกี่ยวข้อง **
ตัดข้อความด้วย Split
ฟังก์ชันค้นหาคำ
การอ่าน Microsoft Office Word จากตำแหน่งใน เอกสาร
 

Posted on Wednesday, September 04, 2013 by nuyingnaja


การแทนที่คำ ด้วย ฟังก์ชัน Replace

1. Replace() แทนที่คำที่ต้องการ


Syntax
Replace(ByVal Expression As String,ByVal Find As String, ByVal Replacement As String)


Example

ตัวอย่างคำ  Helly gyygle ให้แทน y เป็น 0 จะได้ัดังนี้
 
 Helly gyygle = Hello google

Coding in vb.net

Dim word as String = "Helly gyygle"
Dim str as String = Replace(word , "y", "o")   ' Hello google
Dim str2 as String = word .Replace("y", "o")  ' Hello google

Posted on Wednesday, September 04, 2013 by nuyingnaja

9/03/2013


แปลง ตัวหนังสือเป็นตัวเลข ใน vb.net เขียน Code ได้ดังนี้

        Dim str As String = "50.50"
        Dim str2 As String = "1,050.49"

        Dim Conv_num As Integer = CInt(str)    '  50
        Dim Conv_num2 As Double = CInt(str)    '  50.0

        Dim Conv_num3 As Integer = Val(str)   '  50
        Dim Conv_num4 As Double = Val(str)    '  50.5

        Dim Conv_num5 As Double = CInt(str2)  '  1050.0
        Dim Conv_num6 As Double = Val(str2)   '  1

        ' * หมายเหตุที่ Conv_num6 = 1 เพราะเจอเครื่องหมาย , ใน str2 จึงอ่านค่าไม่ถูกต้องการแก้ไขให้ถูก ให้แก้โดยใช้ ฟังก์ชัน Replace แทนที่คำ ดังนี้

        ' * แทนที่ , ด้วยค่า null ดังนี้

        Dim Conv_num9 As Double = Val(Replace(str2, ",", ""))  ' 1050.49
        '========= END  ===========

*** บทความที่เกี่ยวข้อง ***
Convert Integer  to String
ฟังก์ชัน Replace แทนที่คำ

Posted on Tuesday, September 03, 2013 by nuyingnaja


แปลง ตัวเลขเป็นตัวหนังสือ ใน vb.net เขียน Code ได้ดังนี้

Dim num As Integer = 500
Dim num2 As Decimal = 2550.49

Dim Convstr As String = CStr(num)     '  500
Dim Convstr2 As String = CStr(num2)   '  2550.49
Dim Conv_num7 As String = Integer.Parse(num)  ' 500
Dim Conv_num8 As String = Decimal.Parse(num2) ' 2550.49

*** บทความที่เกี่ยวข้อง ***
Convert String to Integer


Posted on Tuesday, September 03, 2013 by nuyingnaja