如何在vb.net中拆分字符串[重复]

时间:2022-10-05 19:19:24

Possible Duplicate:
How to split in vb.net

可能重复:如何在vb.net中拆分

I have a string "1- ABCDEFGH - HIJKLMN - 1"

我有一个字符串“1- ABCDEFGH - HIJKLMN - 1”

I just want to get "1" out of the string (The first number before "-" in the string)

我只想从字符串中得到“1”(字符串中“ - ”之前的第一个数字)

如何在vb.net中拆分字符串[重复]

3 个解决方案

#1


2  

Use String.Split()

使用String.Split()

Dim s = "1- ABCDEFGH - HIJKLMN - 1"
Dim one = s.Split("-"c)(0) ' one = 1

#2


1  

Well you can treat a string in .Net like an array, so you can get the first character like so:

那么你可以将.Net中的字符串视为一个数组,所以你可以像这样得到第一个字符:

Sub Main()
    Dim myString As String = "1- ABCDEFGH - HIJKLMN -1"
    Dim firstCharacter As String = myString(0)
End Sub

#3


0  

String stringPart = myString.Substring(0,myString.IndexOf('-')) will put in stringPart all characters beginning from the start of the string (index 0) up to the first character '-' (not included)

String stringPart = myString.Substring(0,myString.IndexOf(' - '))将在stringPart中放入从字符串开头(索引0)到第一个字符' - '(不包括)的所有字符

#1


2  

Use String.Split()

使用String.Split()

Dim s = "1- ABCDEFGH - HIJKLMN - 1"
Dim one = s.Split("-"c)(0) ' one = 1

#2


1  

Well you can treat a string in .Net like an array, so you can get the first character like so:

那么你可以将.Net中的字符串视为一个数组,所以你可以像这样得到第一个字符:

Sub Main()
    Dim myString As String = "1- ABCDEFGH - HIJKLMN -1"
    Dim firstCharacter As String = myString(0)
End Sub

#3


0  

String stringPart = myString.Substring(0,myString.IndexOf('-')) will put in stringPart all characters beginning from the start of the string (index 0) up to the first character '-' (not included)

String stringPart = myString.Substring(0,myString.IndexOf(' - '))将在stringPart中放入从字符串开头(索引0)到第一个字符' - '(不包括)的所有字符