关于索引和计数的帮助必须引用字符串上的位置 [英] Help on index and count must refer to a location on the string

查看:82
本文介绍了关于索引和计数的帮助必须引用字符串上的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,请尝试从我从外部文件加载的一组字符串中删除一些字符串.我可以使用while循环遍历文件中的所有数据.遍历数据后,我想删除字符串的第一部分并向其中添加另一个字符串.例如,如果这是从文件加载的数据.
0803456789
0805566788
0802345666
0802334453
0804528721
循环遍历后,我想删除每行的第一个字符串.像第一行一样,我将删除前面的0并添加234.给我一个新的字符串,例如234803456789.我现在可以解决所有问题是它给出了一个错误,说索引和计数必须引用字符串上的一个位置.不知道该怎么做.请高度赞赏.谢谢,这是我到目前为止的代码.

Hello please i am trying to remove some string from a set of string which i loaded from an external file. I am able to loop through all the data in the file with a while loop. After looping through the data i want to remove the first part of the string and add another string to it. For example if this are the data loaded from from the file.
0803456789
0805566788
0802345666
0802334453
0804528721
After looping through i want to remove the first string of each line.Like the first line i will remove the 0 in front and add 234 to it.To give me a new string like 234803456789.I am able to do all that the problem now is that it gives an error saying index and count must refer to a location on the string.Don''t know what more to do.Please any help is highly appreciated.Thanks,this is my code so far.

Sub Main()
        Using myreader As StreamReader = New StreamReader("C:\Users\Jamiebones\Desktop\learning_application\learning_application\Phone Number.txt")

            While Not (myreader.EndOfStream)


                Dim line As String = myreader.ReadLine
                Dim jam As String = ""
                jam = "234" & line.Remove(0, 1)


                Console.WriteLine(jam)

            End While
            Console.ReadLine()
            
        End Using



添加了代码块[/编辑]



Code block added[/Edit]

推荐答案

尝试此操作

try this

Sub Main()
    Using myreader As StreamReader = New StreamReader("
C:\Users\Jamiebones\Desktop\learning_application\learning_application\Phone Number.txt")

        While Not (myreader.EndOfStream)
            Dim line As String = myreader.ReadLine.Trim 'remove white space
            Dim strBuilder As New Text.StringBuilder 'better for performance
            If String.IsNullOrEmpty(line) = False AndAlso line.Length > 1 Then
                strBuilder.Append("234")
                strBuilder.Append(line.Remove(0, 1))
            End If
            Console.WriteLine(strBuilder.ToString)
        End While
        Console.ReadLine()

    End Using

End Sub


这篇关于关于索引和计数的帮助必须引用字符串上的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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