VB.Net Tutorial/Data Type/String Split

Материал из VB Эксперт
Версия от 12:54, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

String Split

Option Strict On
 Imports System
 Class Tester
     Public Shared Sub Main( )
         Dim s1 As String = "One,Two,Three Liberty Associates, Inc."
         Const Space As Char = " "c
         Const Comma As Char = ","c
         Dim delimiters( ) As Char = {Space, Comma}
         Dim output As String = ""
         Dim ctr As Integer = 0
         Dim resultArray As String( ) = s1.Split(delimiters)
         Dim subString As String
         For Each subString In resultArray
             ctr = ctr + 1
             output &= ctr.ToString( )
             output &= ": "
             output &= subString
             output &= Environment.NewLine
         Next subString
         Console.WriteLine(output)
     End Sub "Main
 End Class "Tester
1: One
2: Two
3: Three
4: Liberty
5: Associates
6:
7: Inc.

String split by using the Split, String.Split()

Public Class Tester
    Public Shared Sub Main
        Dim quote As String = "The important thing is not to " & _
            "stop questioning. --Albert Einstein"
        Dim strArray1() As String = Split(quote, "ing")
        Dim strArray2() As String = quote.Split(CChar("ing"))
        Dim counter As Integer
        For counter = 0 To strArray1.Length - 1
            Console.WriteLine(strArray1(counter))
        Next counter

        For counter = 0 To strArray2.Length - 1
            Console.WriteLine(strArray2(counter))
        Next counter
        
    End Sub

End Class
The important th
 is not to stop question
. --Albert Einstein
The
mportant th
ng
s not to stop quest
on
ng. --Albert E
nste
n

Use Regular Expression to Split string

Imports System
 Imports System.Text
 Imports System.Text.RegularExpressions

 Class Tester
     Public Shared Sub Main( )
         Dim s1 As String = "One,Two,Three Four"
         Dim theRegex As New Regex(" |, |,")
         Dim sBuilder As New StringBuilder( )
         Dim id As Integer = 1
         Dim subString As String
         For Each subString In theRegex.Split(s1)
             id = id + 1
             sBuilder.AppendFormat("{0}: {1}" & Environment.NewLine, id, subString)
         Next subString
         Console.WriteLine("{0}", sBuilder)
     End Sub
 End Class
2: One
3: Two
4: Three
5: Four