[C#][VB.NET][VB6][JAVA] 字串分割

  • 34543
  • 0
  • C#
  • 2008-11-16

摘要:[C#][VB.NET][VB6][JAVA] 字串分割

我們可以使用System命名空間.String類別.Split方法,來進行字串切割的處理。

VB.NET

法一:使用System.String.Split

Module Module1
    Sub Main()
        Dim delimiterChars() As Char = {"!", "@", "#", "$", "%"}
        Dim text As String = "one!two@three#four$five"
        Dim words() As String = text.Split(delimiterChars)
        Dim s
        For Each s In words
            System.Console.WriteLine(s)
        Next
    End Sub

End Module

法二:使用Split

Module Module1
    Sub Main()
        Dim text As String = "one!two!three!four!five"
        Dim words() As String = Split(text, "!")
        Dim s
        For Each s In words
            System.Console.WriteLine(s)
        Next
    End Sub

End Module

C#

class TestStringSplit
{
    static void Main()
    {
        char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
        string text = "one\ttwo three:four,five six seven";
        string[] words = text.Split(delimiterChars);
        foreach (string s in words)
        {
            System.Console.WriteLine(s);
        }

    }

}

MSDN:http://msdn.microsoft.com/zh-tw/library/system.string.split.aspx

JAVA:

public class TTT {
    public static void main(String[] args) {
  String s = new String("2_8_7_4_3_9_1");
  String[] arr = s.split("_");
  System.out.println(s);
  System.out.println(arr[0]);
  System.out.println(arr[1]);
  System.out.println(arr[2]);
  System.out.println(arr[3]);
    }

}

參考資料:http://hain.javaeye.com/blog/74853

VB6:

Option Explicit
Private Sub Form_Load()
    Dim texts As String
    texts = "one!two!three!four!five"
    Dim words() As String
    words = Split(texts, "!")
    Dim s
    For Each s In words
        Debug.Print (s)
    Next
End Sub

 

 

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo