0

我有一个整数数据的文本文件,即

00201305170013333
01201305170013333
02201305170013333

我想将每行的第 2 和第 3 位置与每行上的第 6 和第 7 索引交换。我应用了交换技术,但不知何故新字符串没有得到更新。

看代码:

Imports System
Imports System.IO
Imports System.Collections
Module Module1
    Sub Main()
        Dim reader As StreamReader = New StreamReader("input.txt")
        Dim sLine As String = ""
        Dim arrText As New ArrayList()
        Do
            sLine = reader.ReadLine()
            If Not sLine Is Nothing Then
                arrText.Add(sLine)
            End If
        Loop Until sLine Is Nothing
        reader.Close()
        Dim varReplace As Integer
        Dim arrTextToString(arrText.Count) As String
        For varReplace = 0 To arrText.Count - 1
            arrTextToString(varReplace) = arrText(varReplace).ToString()
        Next

                Dim rep As Integer
                For rep = 0 To arrText.Count - 1
                    Dim two, three, mix1, mix2, six, seven As String
                    Dim str = arrText(rep)
                    two = (str(2))
                    three = (str(3))
                    six = (str(6))
                    seven = (str(7))
                    mix1 = two
                    two = six
                    six = mix1
                    mix2 = three
                    three = seven
                    seven = mix2


                    str(2) = two.ToString()
                    str(3) = three.ToString()
                    str(6) = six.ToString()
                    str(7) = seven.ToString()

                    Console.Write(two)
                    Console.Write(three)
                    Console.Write(" ")
                    Console.Write(six)
                    Console.Write(seven)
                    Console.Write(" ")
                    Console.WriteLine(str)
                Next
                'For pri = 0 To arrText.Count - 1
                'Console.WriteLine(arrTextToString(pri))
                'Next


        Console.ReadLine()
    End Sub
End Module
4

3 回答 3

0

您正在操作一个字符串。字符串不会自动成为指向原始对象的指针。这基本上就是你正在做的事情。

  • 循环遍历字符串数组。
  • 字符串 str = 数组副本 [rep]。
  • 在 str 上执行更新
  • 结束循环

你需要做的是:

  • arrText[rep] = str

你应该没事。

于 2013-08-29T06:49:05.990 回答
0

字符串是不可变的,不能更改。您可能已经注意到,由于您的代码在尝试更改字符串的一部分的行中产生的编译错误:

str(2) = two.ToString()

结果是:

MissingMemberException:属性“字符”是只读的。

因此,每次要更改字符串时都必须构造新字符串。


另外,我建议使用 aTextFieldParser来读取数据,并使用简单的String.Format.

例子:

Sub Main
    Dim result = From fields In ReadData("c:\your\path\input.txt")
                 Select String.Format("{0}{3}{2}{1}{4}", fields)

    For Each line in result     ' note that result is lazy '
        Console.WriteLine(line) ' Do whatever '
    Next
End Sub

Iterator Function ReadData(filename As String) As IEnumerable(Of String())
    Using parser = new TextFieldParser(filename) With { .TextFieldType = FieldType.FixedWidth }
        ' read the data in fields of a specific length '
        ' so we can later simply swap the 2nd with the 4th field '
        parser.SetFieldWidths(1, 2, 2, 2, -1)
        While Not parser.EndOfData
            Yield parser.ReadFields()
        End While
    End Using
End Function
于 2013-08-29T08:48:35.773 回答
0

我无法弄清楚为什么您的代码不起作用,但我可以编写一个简单的扩展方法来进行交换,例如:

Imports System.Runtime.CompilerServices
Public Class StringTest
    Implements ITestable

    Public Sub ExecuteTest() Implements ITestable.ExecuteTest
        Dim theString = "00201305170013333"
        Console.WriteLine("old: " & theString)

        Dim chars = theString.ToArray()
        chars.Swap(2, 6)
        chars.Swap(3, 7)
        theString = String.Concat(chars)
        Console.WriteLine("new: " & theString)
    End Sub
End Class

Public Module Extensions
    <Extension()>
    Public Sub Swap(Of T)(records As IList(Of T), idx1 As Integer, idx2 As Integer)
        Dim item1 = records.ElementAt(idx1)
        records(idx1) = records.ElementAt(idx2)
        records(idx2) = item1
    End Sub
End Module
于 2013-08-29T06:37:15.223 回答