SlideShare a Scribd company logo
1 of 4
Download to read offline
VB .NET 2005 By Warawut Khangkhan
การจดการขอม!ลชน%ด String ใน VB.NET 2005 express Edition



             การจดการขอมลชนด String ใน VB .NET 2005 Express Edition
        VB .NET ม0เมธอด (Method) หร7อฟ9งก;ช<น (Function) ท0<ช@วยในการจดการขอม!ลชน%ดสตร%ง (String) ไม@ว@าจะเปHนการ
แปลงตวอกษรตวพ%มพ;เลLกใหเปHนตวอกษรตวพ%มพ;ใหญ@ หร7อจะเปHนการตดช@องว@างทางดานซายหร7อทางดานขวา หร7อทOง 2
ดาน กLสามารถจดการไดอย@างง@ายดาย ซQ<งเมธอด (Method) เหล@าน0Oจะอย!ในคลาส System.String
                                                                  @

ตาราง สรTป Method ท0<สาคญท0เ< ก0<ยวกบการจดการขอม!ลชน%ดสตร%ง
                      U
   Method               Description            Example              Example Program                      Result
ToUpper     แปลงตวอกษรเปHนตวพ%มพ;ใหญ@ “BaBy”                Dim strA As String = “BaBy”          'strB = “BABY”
                                                            Dim strB As String
                                                            strB = strA.ToUpper
ToLower     แปลงตวอกษรเปHนตวพ%มพ;เลLก “BaBy”                Dim strA As String = “BaBy”          'strB = “baby”
                                                            Dim strB As String
                                                            strB = strA.ToLower
Insert      แทรกขอความท0<ตองการลงไป         “VB .NET”       Dim strA As String = “VB .NET”       'strA =
            ในสตร%ง ณ ตUาแหน@งท0ระบT
                                   <                        strA = strA.Insert(3, “test “)           “VB test .NET”
Replace     แทรกขอความท0<ตองการดวยขอ “VB Sixth”             Dim strA As String = “VB Sixth”      'strA = “VB .NET”
            ความใหม@                                        strA =
                                                                strA.Replace(“Sixth”, “.NET “)
Remove      ลบขอความท0<ตองการออก            “VB test .NET” Dim strA As String =                  'strA = “VB .NET”
            จากสตร%ง                                            “VB test .NET”
                                                            strA = strA.Remove(3, 5)
Trim,       ตดช@องว@างทOงหมด,               “ Bird “        Dim strA As String = “ Bird “
TrimStart, ตดช@องว@างเฉพาะดานซาย และ                        strA = strA.Trim                     'strA = “Bird”
TrimEnd     ตดช@องว@างเฉพาะดานขวา                           strA = strA.TrimStart                'strA = “Bird “
                                                            strA = strA.TrimEnd                  'strA = “ Bird”
PadLeft,    เต%มตวอกษรท0<ตองการเขาไปดาน “VB .NET”           Dim strA As String = “VB .NET”
PadRight    ซาย และดานขวาของ                                strA = strA.PadLeft(10, “a”)         'strA=“aaaVB .NET”
            สตร%งตามลUาดบ                                   strA = strA.PadRight(10, “a”)        'strA=”VB .NETaaa”
SubString ดQงขอความ ณ ตUาแหน@งท0<ตอง        “VB .NET”       Dim strA As String = “VB .NET”
            การจากสตร%ง                                     strA = strA.SubString(3)             'strA = “.NET”




                                                          -1-
VB .NET 2005 By Warawut Khangkhan
การจดการขอม!ลชน%ด String ใน VB.NET 2005 express Edition


ตวอย6าง การเข8ยนโปรแกรมในการจดการขอมลชนด String
        ตวอย@างท0จะใชน0OเปHนตวอย@างท0<เก0<ยวกบการรบขอม!ลท0<เปHนช7<อของผ!ใช โดยใช TextBox Control ในการรบขอม!ล ซQ<งค@าท0<
                 <
ไดจะตองไม@เปHนช@องว@าง (Blank) หร7อไม@ไดใส@อะไรเลย โดยเราจะทUาการตรวจสอบขอม!ลท0<รบเขามาจาก TextBox ว@าม0การพ%มพ;
ขอความหร7อไม@ หร7อจะเปHนกดปTpม space bar เพ7<อใหเก%ดช@องว@าง เน7<องจาก Space bar ถ7อว@าเปHนตวอกษรตวหนQ<งเช@นกน เพราะ
ฉะนOนเราตองการทUาการตดช@องว@างออกจากขอความก@อน ถQงจะนUามาตรวจสอบว@าเปHนค@าว@างหร7อไม@


                                                                  2                  3
                                                                                     4



ตาราง กUาหนด Properties ท0<สาคญของแต@ละ Control
                            U
 No.       Control                    Properties                                    Value
  1 Form                      Name                        Form1
                              Text                        โปรแกรมแนะนUาช7<อ
                              StartPosition               CenterScreen
                              MaximizeBox                 False
                              MinimumBox                  False
  2 TextBox                   Name                        txtName
  3 Button                    Name                        btnOk
                              Text                        ตกลง
  4 Button                    Name                        btnExit
                              Text                        ออก




                                                            -2-
VB .NET 2005 By Warawut Khangkhan
การจดการขอม!ลชน%ด String ใน VB.NET 2005 express Edition


Coding:

Public Class Form1

  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
     Application.Exit()
  End Sub

  Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
     If txtName.Text.Trim <> "" Then
        MessageBox.Show("สวสด คณ" & txtName.Text.Trim, "แนะน ตวเอง", MessageBoxButtons.OK, MessageBoxIcon.
Information)
     Else
        MessageBox.Show("ขอโทษดวยครบ" & vbCrLf & "คณตองพมพชอของคณก#อน", "แนะน ตวเอง", MessageBoxButtons.
                                                                !
OK, MessageBoxIcon.Information)
     End If
     txtName.Focus()
  End Sub

  Private Sub txtName_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.Enter
     txtName.SelectionStart = 0
     txtName.SelectionLength = txtName.Text.Length
  End Sub

  Private Sub txtName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Handles txtName.KeyDown
     If e.KeyCode = Keys.Enter Then
       btnOk_Click(sender, e) 'เรยกใช Method ของป&ม "ตกลง"

       txtName.SelectionStart = 0
       txtName.SelectionLength = txtName.Text.Length
     End If
  End Sub
End Class




                                                          -3-
VB .NET 2005 By Warawut Khangkhan
การจดการขอม!ลชน%ด String ใน VB.NET 2005 express Edition


Text Program:

กรณ0ท0<ม0ขอม!ลใน TextBox จะไดผลลพธ; ดงร!ปดานล@าง           กรณ0ทไม@ม0ขอม!ลใน TextBox จะไดผลลพธ; ดงร!ปดานล@าง
                                                                0<




                                                          -4-

More Related Content

More from Warawut

Database design
Database designDatabase design
Database designWarawut
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4Warawut
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10Warawut
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9Warawut
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8Warawut
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7Warawut
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6Warawut
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6Warawut
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5Warawut
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4Warawut
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5Warawut
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3Warawut
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3Warawut
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2Warawut
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemWarawut
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4Warawut
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1Warawut
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MISWarawut
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3Warawut
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2Warawut
 

More from Warawut (20)

Database design
Database designDatabase design
Database design
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information System
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MIS
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2
 

การจัดการข้อมูลชนิด String ใน VB.NET 2005 Express Editor

  • 1. VB .NET 2005 By Warawut Khangkhan การจดการขอม!ลชน%ด String ใน VB.NET 2005 express Edition การจดการขอมลชนด String ใน VB .NET 2005 Express Edition VB .NET ม0เมธอด (Method) หร7อฟ9งก;ช<น (Function) ท0<ช@วยในการจดการขอม!ลชน%ดสตร%ง (String) ไม@ว@าจะเปHนการ แปลงตวอกษรตวพ%มพ;เลLกใหเปHนตวอกษรตวพ%มพ;ใหญ@ หร7อจะเปHนการตดช@องว@างทางดานซายหร7อทางดานขวา หร7อทOง 2 ดาน กLสามารถจดการไดอย@างง@ายดาย ซQ<งเมธอด (Method) เหล@าน0Oจะอย!ในคลาส System.String @ ตาราง สรTป Method ท0<สาคญท0เ< ก0<ยวกบการจดการขอม!ลชน%ดสตร%ง U Method Description Example Example Program Result ToUpper แปลงตวอกษรเปHนตวพ%มพ;ใหญ@ “BaBy” Dim strA As String = “BaBy” 'strB = “BABY” Dim strB As String strB = strA.ToUpper ToLower แปลงตวอกษรเปHนตวพ%มพ;เลLก “BaBy” Dim strA As String = “BaBy” 'strB = “baby” Dim strB As String strB = strA.ToLower Insert แทรกขอความท0<ตองการลงไป “VB .NET” Dim strA As String = “VB .NET” 'strA = ในสตร%ง ณ ตUาแหน@งท0ระบT < strA = strA.Insert(3, “test “) “VB test .NET” Replace แทรกขอความท0<ตองการดวยขอ “VB Sixth” Dim strA As String = “VB Sixth” 'strA = “VB .NET” ความใหม@ strA = strA.Replace(“Sixth”, “.NET “) Remove ลบขอความท0<ตองการออก “VB test .NET” Dim strA As String = 'strA = “VB .NET” จากสตร%ง “VB test .NET” strA = strA.Remove(3, 5) Trim, ตดช@องว@างทOงหมด, “ Bird “ Dim strA As String = “ Bird “ TrimStart, ตดช@องว@างเฉพาะดานซาย และ strA = strA.Trim 'strA = “Bird” TrimEnd ตดช@องว@างเฉพาะดานขวา strA = strA.TrimStart 'strA = “Bird “ strA = strA.TrimEnd 'strA = “ Bird” PadLeft, เต%มตวอกษรท0<ตองการเขาไปดาน “VB .NET” Dim strA As String = “VB .NET” PadRight ซาย และดานขวาของ strA = strA.PadLeft(10, “a”) 'strA=“aaaVB .NET” สตร%งตามลUาดบ strA = strA.PadRight(10, “a”) 'strA=”VB .NETaaa” SubString ดQงขอความ ณ ตUาแหน@งท0<ตอง “VB .NET” Dim strA As String = “VB .NET” การจากสตร%ง strA = strA.SubString(3) 'strA = “.NET” -1-
  • 2. VB .NET 2005 By Warawut Khangkhan การจดการขอม!ลชน%ด String ใน VB.NET 2005 express Edition ตวอย6าง การเข8ยนโปรแกรมในการจดการขอมลชนด String ตวอย@างท0จะใชน0OเปHนตวอย@างท0<เก0<ยวกบการรบขอม!ลท0<เปHนช7<อของผ!ใช โดยใช TextBox Control ในการรบขอม!ล ซQ<งค@าท0< < ไดจะตองไม@เปHนช@องว@าง (Blank) หร7อไม@ไดใส@อะไรเลย โดยเราจะทUาการตรวจสอบขอม!ลท0<รบเขามาจาก TextBox ว@าม0การพ%มพ; ขอความหร7อไม@ หร7อจะเปHนกดปTpม space bar เพ7<อใหเก%ดช@องว@าง เน7<องจาก Space bar ถ7อว@าเปHนตวอกษรตวหนQ<งเช@นกน เพราะ ฉะนOนเราตองการทUาการตดช@องว@างออกจากขอความก@อน ถQงจะนUามาตรวจสอบว@าเปHนค@าว@างหร7อไม@ 2 3 4 ตาราง กUาหนด Properties ท0<สาคญของแต@ละ Control U No. Control Properties Value 1 Form Name Form1 Text โปรแกรมแนะนUาช7<อ StartPosition CenterScreen MaximizeBox False MinimumBox False 2 TextBox Name txtName 3 Button Name btnOk Text ตกลง 4 Button Name btnExit Text ออก -2-
  • 3. VB .NET 2005 By Warawut Khangkhan การจดการขอม!ลชน%ด String ใน VB.NET 2005 express Edition Coding: Public Class Form1 Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Application.Exit() End Sub Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click If txtName.Text.Trim <> "" Then MessageBox.Show("สวสด คณ" & txtName.Text.Trim, "แนะน ตวเอง", MessageBoxButtons.OK, MessageBoxIcon. Information) Else MessageBox.Show("ขอโทษดวยครบ" & vbCrLf & "คณตองพมพชอของคณก#อน", "แนะน ตวเอง", MessageBoxButtons. ! OK, MessageBoxIcon.Information) End If txtName.Focus() End Sub Private Sub txtName_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.Enter txtName.SelectionStart = 0 txtName.SelectionLength = txtName.Text.Length End Sub Private Sub txtName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyDown If e.KeyCode = Keys.Enter Then btnOk_Click(sender, e) 'เรยกใช Method ของป&ม "ตกลง" txtName.SelectionStart = 0 txtName.SelectionLength = txtName.Text.Length End If End Sub End Class -3-
  • 4. VB .NET 2005 By Warawut Khangkhan การจดการขอม!ลชน%ด String ใน VB.NET 2005 express Edition Text Program: กรณ0ท0<ม0ขอม!ลใน TextBox จะไดผลลพธ; ดงร!ปดานล@าง กรณ0ทไม@ม0ขอม!ลใน TextBox จะไดผลลพธ; ดงร!ปดานล@าง 0< -4-