长度不能小于零 [英] Length cannot be less than zero

查看:395
本文介绍了长度不能小于零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我有一个程序而且我遇到了一些错误。从我的程序中Dim fileoutput As String = TextBox2.Text.Substring(0,TextBox2.Text.Length - 4)中有错误,请帮助我学习。谢谢,抱歉我的英语不好。



我尝试过的事情:



 Private Sub namafiledeskrip()
Dim posisi As Integer = 0
Dim t As Integer = < span class =code-digit> 0

TextBox2.Text.IndexOf( \c,t)<> -1
posisi = TextBox2.Text.IndexOf(
\ c,t)
t = posisi + 1
结束

Dim fileoutput As String = TextBox2.Text.Substring(0,TextBox2.Text.Length - 4)
fileoutput = fileoutput .substring((posisi + 1))
RichTextBox2.Text = fileoutput.Replace(
_ c, c)


End Sub

解决方案

错误信息非常明确:长度不能小于零

由于您根据文本框内容传递长度:

  Dim  fileoutput  As   String  = TextBox2.Text.Substring( 0 ,Text Box2.Text.Length  -   4 



问题几乎可以肯定,文本框包含三个字符或less ...


根据错误消息,显然你的TextBox2.Text.Length小于4,导致它们的减法产生负值。对于Substring()方法的第二个参数,你不能有负长度。

看起来你试图从文件路径中删除点扩展名,如果是这样,你应该首先确定那个点扩展名存在,例如

 Dim  value  As  String  =   peterleow / haha​​.html 

如果 value .LastIndexOf( c)< ;> -1然后
Console.WriteLine( value .Substring( 0 value .LastIndexOf( c)))
结束如果





学习 LastIndexOf [ ^ ]


< blockquote>

 TextBox2.Text.Substring( 0 ,TextBox2.Text.Length  -   4 



这个提取包含 TextBox2 减去最后4个字符,这意味着它会失败如果没有至少4个字符。

为了证明你的代码,你给了检查长度是否超过4并告诉你该做什么。


Excuse me, I have a program and I got some error. there Is error in "Dim fileoutput As String = TextBox2.Text.Substring(0, TextBox2.Text.Length - 4)" from my program, please help me for my studying. thank you, sorry for my bad English.

What I have tried:

Private Sub namafiledeskrip()
        Dim posisi As Integer = 0
        Dim t As Integer = 0

        While TextBox2.Text.IndexOf("\"c, t) <> -1
            posisi = TextBox2.Text.IndexOf("\"c, t)
            t = posisi + 1
        End While

        Dim fileoutput As String = TextBox2.Text.Substring(0, TextBox2.Text.Length - 4)
        fileoutput = fileoutput.Substring((posisi + 1))
        RichTextBox2.Text = fileoutput.Replace("_"c, "."c)


    End Sub

解决方案

The error message is pretty explicit: "Length cannot be less than zero"
Since you are passing the length based on the Textbox content:

Dim fileoutput As String = TextBox2.Text.Substring(0, TextBox2.Text.Length - 4)


The problem is almost certainly that the text box contains three characters or less...


Based on the error message, apparently your TextBox2.Text.Length is less than 4 which resulted in negative value from their subtraction. You can't have a negative length for the second argument of Substring() method.
Look like you are trying to remove the dot extension from your file path, if so, you should first ascertain that that dot extension exists, e.g.

Dim value As String = "peterleow/haha.html"

If value.LastIndexOf("."c) <> -1 Then
    Console.WriteLine(value.Substring(0, value.LastIndexOf("."c)))
End If



Learn LastIndexOf[^]


TextBox2.Text.Substring(0, TextBox2.Text.Length - 4)


This extract the contain of TextBox2 minus the last 4 chars, this imply that it will fail if there is not at least 4 chars.
To proof your code, you gave to check if length is more than 4 and tell what to do otherwise.


这篇关于长度不能小于零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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