2014-09-03 107 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