本文整理汇总了VB.NET中System.String.Split方法的典型用法代码示例。如果您正苦于以下问题:VB.NET String.Split方法的具体用法?VB.NET String.Split怎么用?VB.NET String.Split使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.Split方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Main
' 导入命名空间
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim expressions() As String = { "16 + 21", "31 * 3", "28 / 3",
"42 - 18", "12 * 7",
"2, 4, 6, 8" }
Dim pattern As String = "(\d+)\s+([-+*/])\s+(\d+)"
For Each expression In expressions
For Each m As Match in Regex.Matches(expression, pattern)
Dim value1 As Integer = Int32.Parse(m.Groups(1).Value)
Dim value2 As Integer = Int32.Parse(m.Groups(3).Value)
Select Case m.Groups(2).Value
Case "+"
Console.WriteLine("{0} = {1}", m.Value, value1 + value2)
Case "-"
Console.WriteLine("{0} = {1}", m.Value, value1 - value2)
Case "*"
Console.WriteLine("{0} = {1}", m.Value, value1 * value2)
Case "/"
Console.WriteLine("{0} = {1:N2}", m.Value, value1 / value2)
End Select
Next
Next
End Sub
End Module
输出:
16 + 21 = 37 31 * 3 = 93 28 / 3 = 9.33 42 - 18 = 24 12 * 7 = 84
示例2: Main
' 导入命名空间
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = String.Format("[This is captured{0}text.]" +
"{0}{0}[{0}[This is more " +
"captured text.]{0}{0}" +
"[Some more captured text:" +
"{0} Option1" +
"{0} Option2][Terse text.]",
vbCrLf)
Dim pattern As String = "\[([^\[\]]+)\]"
Dim ctr As Integer = 0
For Each m As Match In Regex.Matches(input, pattern)
ctr += 1
Console.WriteLine("{0}: {1}", ctr, m.Groups(1).Value)
Next
End Sub
End Module
输出:
1: This is captured text. 2: This is more captured text. 3: Some more captured text: Option1 Option2 4: Terse text.
示例3: Example
' 导入命名空间
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "abacus -- alabaster - * - atrium -+- " +
"any -*- actual - + - armoir - - alarm"
Dim pattern As String = "\s-\s?[+*]?\s?-\s"
Dim elements() As String = Regex.Split(input, pattern)
For Each element In elements
Console.WriteLine(element)
Next
End Sub
End Module
输出:
abacus alabaster atrium any actual armoir alarm
示例4: Main
' 导入命名空间
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim value As String = "This is the first sentence in a string. " +
"More sentences will follow. For example, " +
"this is the third sentence. This is the " +
"fourth. And this is the fifth and final " +
"sentence."
Dim sentences As New List(Of String)
Dim position As Integer = 0
Dim start As Integer = 0
' Extract sentences from the string.
Do
position = value.IndexOf("."c, start)
If position >= 0 Then
sentences.Add(value.Substring(start, position - start + 1).Trim())
start = position + 1
End If
Loop While position > 0
' Display the sentences.
For Each sentence In sentences
Console.WriteLine(sentence)
Next
End Sub
End Module
输出:
This is the first sentence in a string. More sentences will follow. For example, this is the third sentence. This is the fourth. And this is the fifth and final sentence.
示例5: Sample
' This example demonstrates the String() methods that use
' the StringSplitOptions enumeration.
Class Sample
Public Shared Sub Main()
Dim s1 As String = ",ONE,,TWO,,,THREE,,"
Dim s2 As String = "[stop]" & _
"ONE[stop][stop]" & _
"TWO[stop][stop][stop]" & _
"THREE[stop][stop]"
Dim charSeparators() As Char = {","c}
Dim stringSeparators() As String = {"[stop]"}
Dim result() As String
' ------------------------------------------------------------------------------
' Split a string delimited by characters.
' ------------------------------------------------------------------------------
Console.WriteLine("1) Split a string delimited by characters:" & vbCrLf)
' Display the original string and delimiter characters.
Console.WriteLine("1a )The original string is ""{0}"".", s1)
Console.WriteLine("The delimiter character is '{0}'." & vbCrLf, charSeparators(0))
' Split a string delimited by characters and return all elements.
Console.WriteLine("1b) Split a string delimited by characters and " & _
"return all elements:")
result = s1.Split(charSeparators, StringSplitOptions.None)
Show(result)
' Split a string delimited by characters and return all non-empty elements.
Console.WriteLine("1c) Split a string delimited by characters and " & _
"return all non-empty elements:")
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' Split the original string into the string and empty string before the
' delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("1d) Split a string delimited by characters and " & _
"return 2 elements:")
result = s1.Split(charSeparators, 2, StringSplitOptions.None)
Show(result)
' Split the original string into the string after the delimiter and the
' remainder of the original string after the delimiter.
Console.WriteLine("1e) Split a string delimited by characters and " & _
"return 2 non-empty elements:")
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' ------------------------------------------------------------------------------
' Split a string delimited by another string.
' ------------------------------------------------------------------------------
Console.WriteLine("2) Split a string delimited by another string:" & vbCrLf)
' Display the original string and delimiter string.
Console.WriteLine("2a) The original string is ""{0}"".", s2)
Console.WriteLine("The delimiter string is ""{0}""." & vbCrLf, stringSeparators(0))
' Split a string delimited by another string and return all elements.
Console.WriteLine("2b) Split a string delimited by another string and " & _
"return all elements:")
result = s2.Split(stringSeparators, StringSplitOptions.None)
Show(result)
' Split the original string at the delimiter and return all non-empty elements.
Console.WriteLine("2c) Split a string delimited by another string and " & _
"return all non-empty elements:")
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)
Show(result)
' Split the original string into the empty string before the
' delimiter and the remainder of the original string after the delimiter.
Console.WriteLine("2d) Split a string delimited by another string and " & _
"return 2 elements:")
result = s2.Split(stringSeparators, 2, StringSplitOptions.None)
Show(result)
' Split the original string into the string after the delimiter and the
' remainder of the original string after the delimiter.
Console.WriteLine("2e) Split a string delimited by another string and " & _
"return 2 non-empty elements:")
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
Show(result)
End Sub
' Display the array of separated strings.
Public Shared Sub Show(ByVal entries() As String)
Console.WriteLine("The return value contains these {0} elements:", entries.Length)
Dim entry As String
For Each entry In entries
Console.Write("<{0}>", entry)
Next entry
Console.Write(vbCrLf & vbCrLf)
End Sub
End Class
输出:
1) Split a string delimited by characters: 1a )The original string is ",ONE,,TWO,,,THREE,,". The delimiter character is ','. 1b) Split a string delimited by characters and return all elements: The return value contains these 9 elements: <><> <><> <><> 1c) Split a string delimited by characters and return all non-empty elements: The return value contains these 3 elements: 1d) Split a string delimited by characters and return 2 elements: The return value contains these 2 elements: <> 1e) Split a string delimited by characters and return 2 non-empty elements: The return value contains these 2 elements: 2) Split a string delimited by another string: 2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]". The delimiter string is "[stop]". 2b) Split a string delimited by another string and return all elements: The return value contains these 9 elements: <> <> <><> <><> 2c) Split a string delimited by another string and return all non-empty elements: The return value contains these 3 elements: 2d) Split a string delimited by another string and return 2 elements: The return value contains these 2 elements: <> 2e) Split a string delimited by another string and return 2 non-empty elements: The return value contains these 2 elements:
示例6: words
Dim phrase As String = "The quick brown fox"
Dim words() As String
words = phrase.Split(TryCast(Nothing, Char()), 3,
StringSplitOptions.RemoveEmptyEntries)
words = phrase.Split(New Char() {}, 3,
StringSplitOptions.RemoveEmptyEntries)
示例7: words
Dim phrase As String = "The quick brown fox"
Dim words() As String
words = phrase.Split(TryCast(Nothing, String()), 3,
StringSplitOptions.RemoveEmptyEntries)
words = phrase.Split(New String() {}, 3,
StringSplitOptions.RemoveEmptyEntries)
示例8: Example
Module Example
Public Sub Main()
Dim source As String = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]"
Dim stringSeparators() As String = {"[stop]"}
Dim result() As String
' Display the original string and delimiter string.
Console.WriteLine("Splitting the string:{0} '{1}'.", vbCrLf, source)
Console.WriteLine()
Console.WriteLine("Using the delimiter string:{0} '{1}'.", _
vbCrLf, stringSeparators(0))
Console.WriteLine()
' Split a string delimited by another string and return all elements.
result = source.Split(stringSeparators, StringSplitOptions.None)
Console.WriteLine("Result including all elements ({0} elements):", _
result.Length)
Console.Write(" ")
For Each s As String In result
Console.Write("'{0}' ", IIf(String.IsNullOrEmpty(s), "<>", s))
Next
Console.WriteLine()
Console.WriteLine()
' Split delimited by another string and return all non-empty elements.
result = source.Split(stringSeparators, _
StringSplitOptions.RemoveEmptyEntries)
Console.WriteLine("Result including non-empty elements ({0} elements):", _
result.Length)
Console.Write(" ")
For Each s As String In result
Console.Write("'{0}' ", IIf(String.IsNullOrEmpty(s), "<>", s))
Next
Console.WriteLine()
End Sub
End Module
输出:
Splitting the string: "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]". Using the delimiter string: "[stop]" Result including all elements (9 elements): <>' 'ONE' '<>' 'TWO' '<>' '<>' 'THREE' '<>' '<>' Result including non-empty elements (3 elements): ONE' 'TWO' 'THREE'
示例9: Example
Module Example
Public Sub Main()
Dim separators() As String = {",", ".", "!", "?", ";", ":", " "}
Dim value As String = "The handsome, energetic, young dog was playing with his smaller, more lethargic litter mate."
Dim words() As String = value.Split(separators, StringSplitOptions.RemoveEmptyEntries)
For Each word In words
Console.WriteLine(word)
Next
End Sub
End Module
输出:
The handsome energetic young dog was playing with his smaller more lethargic litter mate
示例10: words
Dim phrase As String = "The quick brown fox"
Dim words() As String
words = phrase.Split(TryCast(Nothing, String()),
StringSplitOptions.RemoveEmptyEntries)
words = phrase.Split(New String() {},
StringSplitOptions.RemoveEmptyEntries)
示例11: words
Dim phrase As String = "The quick brown fox"
Dim words() As String
words = phrase.Split(TryCast(Nothing, Char()),
StringSplitOptions.RemoveEmptyEntries)
words = phrase.Split(New Char() {},
StringSplitOptions.RemoveEmptyEntries)
示例12: StringSplit2
Public Class StringSplit2
Public Shared Sub Main()
Dim delimStr As String = " ,.:"
Dim delimiter As Char() = delimStr.ToCharArray()
Dim words As String = "one two,three:four."
Dim split As String() = Nothing
Console.WriteLine("The delimiters are -{0}-", delimStr)
Dim x As Integer
For x = 1 To 5
split = words.Split(delimiter, x)
Console.WriteLine(ControlChars.Cr + "count = {0,2} ..............", x)
Dim s As String
For Each s In split
Console.WriteLine("-{0}-", s)
Next s
Next x
End Sub
End Class
输出:
The delimiters are - ,.:- count = 1 .............. -one two,three:four.- count = 2 .............. -one- -two,three:four.- count = 3 .............. -one- -two- -three:four.- count = 4 .............. -one- -two- -three- -four.- count = 5 .............. -one- -two- -three- -four- --
示例13: SplitTest
Public Class SplitTest
Public Shared Sub Main()
Dim words As String = "This is a list of words, with: a bit of punctuation" + _
vbTab + "and a tab character."
Dim split As String() = words.Split(New [Char]() {" "c, ","c, "."c, ":"c, CChar(vbTab) })
For Each s As String In split
If s.Trim() <> "" Then
Console.WriteLine(s)
End If
Next s
End Sub
End Class
输出:
This is a list of words with a bit of punctuation and a tab character
示例14: Example
Module Example
Public Sub Main()
Dim value As String = "This is a short string."
Dim delimiter As Char = "s"c
Dim substrings() As String = value.Split(delimiter)
For Each substring In substrings
Console.WriteLine(substring)
Next
End Sub
End Module
输出:
Thi i a hort tring.
示例15: Tester
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