Dot-Net

VB.NET String.Split 方法?

  • August 1, 2015

我在使用 String.Split 方法時遇到了一些問題,這裡的範例:

Dim tstString As String = "something here -:- URLhere"
Dim newtstString = tstString.Split(" -:- ")
MessageBox.Show(newtstString(0))
MessageBox.Show(newtstString(1))

以上,在 PHP(我的母語!)中會在消息框中返回一些 here 和 URLhere 的內容。

在 VB.NET 中,我得到:

something here

: (colon)

String.Split 是否僅適用於標準字元?我似乎無法弄清楚這一點。我敢肯定這很簡單!

這是您需要做的,以防止將字元串轉換為Char數組。

   Dim text As String = "something here -:-  urlhere"
   Dim parts As String() = text.Split(New String() {" -:- "}, StringSplitOptions.None)

這是System.String您在這種情況下需要使用的成員函式

Public Function Split(ByVal separator As String(), ByVal options As StringSplitOptions) As String()

引用自:https://stackoverflow.com/questions/7919253