VB String.Split 方法 (String[], Int32, StringSplitOptions)
来源:互联网 发布:男生淘宝名字大全霸气 编辑:程序博客网 时间:2024/05/18 01:37
下面的代码示例使用 StringSplitOptions 枚举来包含或排除由 Split 方法生成的子字符串。
' This example demonstrates the String() methods that use
' the StringSplitOptions enumeration.
Imports System
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 'Main
' 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 'Show
End Class 'Sample
'
'This example produces the following results:
'
'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:
'
'
- VB String.Split 方法 (String[], Int32, StringSplitOptions)
- C#String.Split (string[], StringSplitOptions)
- String.Substring 方法 (Int32, Int32)
- Int32.TryParse (String, Int32)
- C#String.Split (string[], StringSplitOptions) 多参数分割得到数组
- 关于String.split()方法
- String.split方法
- 浅谈string.split()方法
- java String.split 方法
- String.split()方法
- JAVA String.split方法
- string中的split方法
- String 的split()方法
- C# String.Split 方法
- String.split方法
- String的split()方法
- Java String.split()方法
- String的split方法
- hibernate主键生成
- 使用ultraedit和cl编译器打造简易c/c++开发环境
- MapXtreme+C#添加标注
- c#写excel文件 小结
- native2ascii,把properties文件的中文转为/uDDDD格式的方法
- VB String.Split 方法 (String[], Int32, StringSplitOptions)
- 呼唤IT企业的个人英雄主义
- 对html table的误解
- 跳转的问题
- 编译使用GDAL的几个链接
- [Linux] pthread_mutex_lock和pthread_spin_lock的性能
- 使用BSD的 socket函数创建socket
- 修改Linux静态路由
- J2EE学习流程