输入文本文件 - 交换整数

时间:2013-08-29 05:57:03

标签: vb.net string

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

  

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

  • 循环遍历字符串数组。
  • String str = copyof array [rep]。
  • 对str
  • 执行更新
  • End Loop

您需要做的是:

  • arrText [rep] = str

你应该没事。

答案 2 :(得分:0)

字符串是不可变的,它们无法更改。您可能已经注意到,通过编译错误,您的代码会在行中生成,您尝试更改字符串的一部分:

str(2) = two.ToString()

结果:

  

MissingMemberException:属性'Chars'是ReadOnly。

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


另外,我建议您使用TextFieldParser来读取数据,并使用简单的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