VB.Net:索引和长度必须指向字符串中的位置。 [英] VB.Net: Index and length must refer to a location within the string.

查看:87
本文介绍了VB.Net:索引和长度必须指向字符串中的位置。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那是我得到的错误。我理解这个错误意味着其中一个数字超出范围 - 但不是!这是我的第一个代码片段(尝试使用子字符串从''Label1''中获取最后四个字符。我称之为子字符串filetype):



That''s the error I get. I understand this error means that one of the numbers is out of range--BUT ITS NOT! Here''s my first code snippet (trying to get the last four characters out of ''Label1'', using a substring. I call this substring "filetype"):

filetype = Label1.Text.Substring(Label1.Text.Length - 4,_ Label1.Text.Length)







这个错误几乎说明Label1的长度超过了Label1的长度。没有意义。当我调用子字符串时,我担心Label1实际上没有任何价值,所以我认为我应该使用什么来获取文本应该是Label1用来获取其文本的内容。 Label1使用Form1.ListBox2.SelectedItem。所以,我的第二个代码片段:








This error is pretty much saying that the length of Label1 exceeds the length of Label1. Makes no sense. I was afraid that Label1 actually has no value when I call the substring, so I thought what I should use to get the text should be what Label1 uses to get its text. Label1 uses Form1.ListBox2.SelectedItem. So, my second code snippet:


filetype = Form1.ListBox2.SelectedItem.ToString.Substring(Form1.ListBox2.SelectedItem.ToString.Length - 4, Form1.ListBox2.SelectedItem.ToString.Length)







我得到同样的错误。那么我看一下细节,它说Label1的文字是51个字符。所以,我的第三个代码片段,只是为了测试它:








And I get the same error. So then I look at the details, and it says that Label1''s text is 51 characters. So, my third code snippet, just to test it:


filetype = Label1.Text.Substring(47, 51)







并且相信或不相同,我得到同样的错误!它清楚地说长度是51.我很难过。



感谢您阅读我的整个问题。



Bill




AND BELIEVE IT OR NOT, I GET THE SAME ERROR! It clearly says the length is 51. I''m just so stumped.

So thanks for reading my whole question.

Bill

推荐答案

您好,



您的代码段:

Hi,

Your code snippet:
filetype = Label1.Text.Substring(47, 51);



startIndex 为47,长度是51. 51 不是子字符串需要结束的地方。 51 是你想要的子串的长度。

在这里阅读更多:

http://msdn.microsoft.com/en-us/library/aka44szs.aspx [ ^ ]


The startIndex is 47, and the length is 51. 51 isn''t where the substring needs to end. 51 is the length of the substring you want to take.
Read more here:
http://msdn.microsoft.com/en-us/library/aka44szs.aspx[^]


确定除非我遗漏了一些明显的东西,否则你只是误解了第二个参数。

它是你想要提取的字符串的长度,而不是长度你的开始。

OK, unless I''m missing something obvious then it''s simply that you''ve misinterpreted the second parameter.
it''s the length of the string you want to extract, not the length of what you started with.
"TheBilly102030".Substring(3, 5) = "Billy"




"TheBilly102030".Substring(14 - 4, 14) = Error

因为要求它从位置10开始的14个字符串中抓取14个字符,所以它会查找10到24之间的字符,其中大部分都不存在...



如果您想要的是最后三个字符,或者最后四个字符,那么最后一个字符后面的所有内容。 然后:



Since you are asking it to grab 14 characters from a 14 character string starting at position 10, so it goes looking for characters between 10 and 24, most of which don''t exist...

If what you want is the last three characters, or the last four, ot everything after the last "." then:

Dim filename as string = "TheBilly102030.doc"
'Change 3 to 4 for a 4 character file extension
Dim filetype as string = filename.Substring(filename.length - 3, 3) = "doc"



变量版本只需抓住最后一个位置即可完成并将其替换为上面使用的字面数字。



MSDN [ ^ ]



Mike


The variable version is done simply by grabbing the position of the last "." and substituting that for the literal number used above.

MSDN[^]

Mike


非常感谢!它现在完美运行!!!
Thanks SO MUCH! It works perfectly now!!!


这篇关于VB.Net:索引和长度必须指向字符串中的位置。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆