2014-09-03 109 views
0

您好,我試圖讓我的串出的部分,並想知道如果有人能幫助所以這是我與vb.net操縱

Dim tempName, NewName As String 

'This is an example of what tempName will Equal 
'What I need is the information in between the first - 
'and the second one. In this case it would be 120 
'I can not do it by number because the dashes are the only thing 
'that stays the same. 
tempName = "3-120-12-6" 

NewName = tempName 'Do not know what String Manipulation to use. 

工作的另一個幾個例子是6- 56.5-12-12我需要56.5或2-89-12-4我需要89 謝謝你的幫助。

+0

閱讀[分割函數(Visual Basic)](http://msdn.microsoft.com/en-us/library/6x627e5f(v = VS.90)的.aspx)。 – PakkuDon 2014-09-03 01:42:48

回答

2

您也可以這樣做...您可以通過檢測第一個-將其分解成小塊。然後得到第二...

Dim x as String = Mid(tempName, InStr(tempName, "-") + 1) 
NewName = Mid(x, 1, InStr(x , "-") - 1) 

或做這樣的東西,把它變成1行代碼......

NewName = Mid(Mid(tempName, InStr(tempName, "-") + 1), 1, InStr(Mid(tempName, InStr(tempName, "-") + 1), "-") - 1) 

或在本最快的方法是使用Split像以下代碼...此代碼將-之間的值等同於數組。既然你想得到第二個值,你會希望數組的索引爲1,因爲數組以0開頭。如果你只是開始,試着創建自己的方式來操縱字符串,這會幫助你的思想的新東西,而不僅僅依靠內置功能...

Dim tempName As String = "3-120-12-6" 
Dim secondname() As String = Split(tempName, "-") 
NewName = secondname(1) 
+0

測試一分鐘。 – Dmcovey1993 2014-09-03 01:53:03

+0

非常感謝你! – Dmcovey1993 2014-09-03 02:33:00

+0

沒問題的朋友:) – 2014-09-03 08:37:48