[Solved] Assign line numbers to items in text


The question may be a case of “I have X and I need Y” where X is the item which needs attention.

If the string really is as you presented it, then

Imports System.Text

Module Module1

    Sub Main()
        Dim s = "{
""0"":{""variable1"":""ABC1"",""variable2"":""AA"",""variable3"":""BB""},
""5"":{""variable1"":""ABC2"",""variable2"":""AA"",""variable3"":""BB""},
""3"":{""variable1"":""BC3"",""variable2"":""AA"",""variable3"":""BB""},
""1"":{""variable1"":""DC4"",""variable2"":""AA"",""variable3"":""BB""},
""4"":{""variable1"":""DD5"",""variable2"":""AA"",""variable3"":""BB""}
}"

        Dim t = s.Split({vbCrLf}, StringSplitOptions.None)

        Dim u As New StringBuilder

        For i = 0 To t.Length - 1
            If t(i).StartsWith("""") Then
                Dim parts = t(i).Split({":"c}, 2)
                If parts.Count = 2 Then
                    u.AppendLine($"""{i + 1}"":{parts(1)}")
                End If

            Else
                u.AppendLine(t(i))
            End If
        Next

        Console.WriteLine(u.ToString().TrimEnd())

        Console.ReadLine()

    End Sub

End Module

outputs:

{
"2":{"variable1":"ABC1","variable2":"AA","variable3":"BB"},
"3":{"variable1":"ABC2","variable2":"AA","variable3":"BB"},
"4":{"variable1":"BC3","variable2":"AA","variable3":"BB"},
"5":{"variable1":"DC4","variable2":"AA","variable3":"BB"},
"6":{"variable1":"DD5","variable2":"AA","variable3":"BB"}
}

5

solved Assign line numbers to items in text