我正在尝试拆分字符串39 -562 -18
,因此它们分别代表XAxis,YAxis和ZAxis。如何将它们分成单个字符串?
答案 0 :(得分:3)
使用Split
。 " "c
表示空格字符。
Dim coordinates As String = "39 -562 -18"
Dim splitCoordinates As String() = coordinates.Split(" "c)
Dim xcoordinate As String = splitCoordinates(0)
Dim ycoordinate As String = splitCoordinates(1)
Dim zcoordinate As String = splitCoordinates(2)
答案 1 :(得分:1)
您也可以这样做:
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim axis = New Axis(" 39 -562 -18")
MessageBox.Show(axis.XAxis)
MessageBox.Show(axis.YAxis)
MessageBox.Show(axis.ZAxis)
End Sub
这是 Axis Class
的代码''' <summary>
''' Axis string Parser Class
''' </summary>
Public Class Axis
Public XAxis
Public YAxis
Public ZAxis
''' <summary>
''' String containing coordinates like " 39 -562 -18"
''' </summary>
Public Sub New(coords As String)
Dim axis = coords.Trim().Split(" ")
XAxis = Convert.ToSingle(axis(0))
YAxis = Convert.ToSingle(axis(1))
ZAxis = Convert.ToSingle(axis(2))
End Sub
End Class
答案 2 :(得分:0)
您可以使用拆分功能
Dim coords = " 39 -562 -18"
Dim axis = coords.Trim().Split(" ")
Dim XAxis = Convert.ToSingle(axis(0))
Dim YAxis = Convert.ToSingle(axis(1))
Dim ZAxis = Convert.ToSingle(axis(2))
答案 3 :(得分:0)
警告我的答案是C#版本而不是VB.NET:
我觉得这很有用,所以我自己也做了一个C#版本。我不确定我的代码有多好,我是新手,但希望这会增加问题的价值。
char[] delimiter = { ' ' };
string text = "39 -562 -18";
string[] words = text.Split(delimiter);
string xcoord = words[0], ycoord = words[1], zcoord = words[2];
ViewBag.Message = "Xcoord: " + xcoord + " Ycoord: " + ycoord + " Zcoord: " + zcoord ;
如果我们不知道分隔符将分隔值,该怎么办? 好吧,只需添加更多分隔符:
char[] delimiter = { ' ', ',', '.', ':', '\t' };
string text = "39,-562:-18";
string[] words = text.Split(delimiter);
string xcoord = words[0], ycoord = words[1], zcoord = words[2];
ViewBag.Message = "Xcoord: " + xcoord + " Ycoord: " + ycoord + " Zcoord: " + zcoord ;
希望这可以帮助任何遇到这个线程的人,就像,该死的,我希望我知道如何在C#中做到这一点... ... lol
所以我只是使用ViewBag进行测试,但没有最后一个viewBag行.. xcoord , ycoord 和 zcoord 应该包含相应的值。