通过搜索字符串找到一行,然后编辑下面2行的行

时间:2015-07-28 15:05:42

标签: vb.net string line

我的第一个问题是,我会尽量保持清醒。

我有一个看起来像这样的文本文件:

[...]
                "tickets"
                {
                    "436"       "320000000400000083421a060100100104"
                    "674"       "320000000400000083421a06010010010a"
                    "292"       "320000000400000083421a0601001001f0"
                    "551"       "320000000400000083421a0601001001da"
                }
                "99550"
                {
                    "informations"      "254"
                    "parameters"        "-banana -lemon"
                }
                "99551"
                {
                    "informations"      "641"
                    "parameters"        "-banana -lemon"
                }
                "550"
                {
                    "informations"      "551"
                    "parameters"        "-banana -lemon"
                }
                "551"
                {
                    "informations"      "123"
                    "parameters"        "-banana -lemon"
                }
                "552"
                {
                    "informations"      "987"
                    "parameters"        "-banana -lemon"
                }
[...]

我想做的是:

  • 在文本文件中搜索此字符串:
    "551" {
  • -apple添加到下面2行的参数行,应该如下所示:
    "parameters" "-banana -lemon -apple"

我认为这是找到这条线的唯一方法,但我没有获得编码技能来完成它。

2 个答案:

答案 0 :(得分:0)

You can try something like this:

Function ReplaceSpecial(ByVal text As String, ByVal find As String, ByVal insert As String) As String
    Dim allLines() As String = Split(text, vbCrLf)
    Dim lineNumber = Array.IndexOf(allLines, find)
    Dim lineToUpdate = Array.FindIndex(Of String)(allLines, lineNumber, Function(f) f.Trim.StartsWith("""parameters"""))
    allLines(lineToUpdate) = allLines(lineToUpdate).Trim.Substring(0, allLines(lineToUpdate).Length - 1) & " " & insert & """"
    Return Join(allLines, vbCrLf)
End Function

Sample Usage:

Assuming that your input text is in a textbox named InputTextBox the following code will show you updated text in OutputTextBox.

OutputTextBox.Text = ReplaceSpecial(InputTextBox.text, """550""", "-apple")

答案 1 :(得分:0)

这是另一个,与Pradeep Kumar的提交没有太大的不同:

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim rs As DAO.Recordset

Dim response As Integer
Dim strSQL As String
Dim strSelect As String
Dim notTypeID As Integer
Dim notItemcode As String
Dim notDate As Date
Dim notAssetId As Long
Dim rcdCount As Integer
Dim i As Integer

'Check the Storage Location query, see if there is a reason to notify the user
    If DCount("*", "qry_UnknownStorageLoc") > 0 Then

        'Set warnings to true to catch any potential errors
        DoCmd.SetWarnings True

        'Getting the record count from the query
        rcdCount = DCount("*", "Notifications", "NotificationTypeID = 1")

        'Set db to the current database and rst to the current recordset from the query qry_OilSolvActNotification
        strSQL = "SELECT AssetID, BarcodeNumber  FROM qry_UnknownStorageLoc WHERE (AssetID NOT IN (SELECT AssetID From Notifications Where NotificationTypeID = 1))"
        Set db = CurrentDb
        Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)

        Debug.Print rst.RecordCount

        i = 1

        'Loop through to gather all the records for this notification type
        'and add them to the Notifications table

        With rst
            Do Until rst.EOF

                'Set the AssetID value, then move to the next record in the query
                notAssetId = rst!AssetId
                notItemcode = rst!BarcodeNumber

                'NotTypeID is the id of the notification type in the NotificationType table
                notTypeID = 1

                rst.MoveNext

                'Setting the notification date to the last date the record was modified with
                'the logic being the last edit triggered the notification. When the record is
                'corrected and/or acknowledged, it will no longer trigger a notification.

                'Null checking to ensure no errors occur

                If (IsNull(DLookup("modifiedon", "qry_UnknownStorageLoc"))) Then
                    notDate = 0
                Else
                    notDate = DLookup("modifiedon", "qry_UnknownStorageLoc")
                End If

                strSelect = "Select * from Notifications WHERE CLng(AssetID) = '" & notAssetId & "' AND ItemCode = '" & notItemcode & "' AND CInt(NotificationTypeID) = '" & CInt(notTypeID) & "'"

                'Set the rs recordset with the records from the table Notifications that match the SQL statement criteria
                Set rs = db.OpenRecordset(strSelect, dbOpenDynaset, dbSeeChanges)

                'Loop the rs recordset. If there is a record to be entered into the Notifications table, insert it.
                With rs
                    If rs.BOF And rs.EOF Then

                        'Set Warnings to false so the user is not presented with a confirmation to add a record every time there is one available.
                        DoCmd.SetWarnings False

                        strSelect = "INSERT INTO Notifications (NotificationTypeID, NotificationDate, AssetID, ItemCode) VALUES('" & notTypeID & "','" & notDate & "','" & notAssetId & "', '" & notItemcode & "');"

                        DoCmd.RunSQL strSelect

                    End If

                    'Close the recordset
                    rs.Close

                End With

                'Clear the recordset
                Set rs = Nothing

                i = i + 1

            Loop
        End With

        'Close and clear the recordset
        rst.Close
        Set rst = Nothing
    End If

    'This is the popup message box that is shown to the user when Fassetrack loads
    response = MsgBox("Notice: " & DCount("*", "Notifications", "NotificationTypeID = 1") & " record(s) which contain an unknown storage location", vbInformation + vbOKOnly, "Fassetrack")