就像标题所说;我想使用像Mid( stringName , startIndex ,[ integerLength ])这样的东西,但不是第三个参数取字符串长度,我希望它采用结束字符索引。所以在像 this
这样的例子中alphabet = "ABCDEFG"
partial = *method I want to use*(alphabet, 2, 4) 'partial would equal "BC"
(如果我的索引编号关闭,请原谅我,但我希望你明白我的意思。)
VB.NET中是否存在类似的内容?
答案 0 :(得分:2)
你想要使用String.Substring
http://msdn.microsoft.com/en-us/library/aka44szs.aspx#Y0
dim alphabet as string = "ABCDEFG"
'partial is a reserved word!
'1,2 is the correct parameters to get 'BC'
dim partialString as string = alphabet.Substring(1, 2) 'partial would equal "BC"
编辑 - 哦,你想做StartIndex,StopIndex不是StartIndex,长度。只需应用一点数学。
dim startIndex as integer = 1
dim stopIndex as integer = 3
'partial would equal "BC"
dim partialString as string = _
alphabet.Substring(startIndex , stopIndex-startIndex )
我将它包装在字符串的扩展方法中,当然给它一个新名称。
答案 1 :(得分:1)
只需使用Mid,长度的数学就很容易(length = endIndex - startIndex
):
part = Mid(alphabet, 2, 4-2)
您也可以使用Substring(使用基于0的索引而不是基于1)来实现相同的功能:
part = alphabet.Substring(1, 3-1);
答案 2 :(得分:-1)
targetstring=alphabet.Substring(2,4)
以上一个应该有效..