簡體   English   中英

從長字符串中提取字符,並使用VB.net關鍵字將輸出重新格式化為CSV

[英]Extract characters from a long string and reformat the output to CSV by using keywords with VB.net

我是VB.Net 2008的新手。我有一個要解決的任務,它是將長字符串中的字符提取到控制台,將提取的文本重新格式化並保存到CSV文件中。 該字符串來自數據庫。

它看起來像: UNH+RAM6957+ORDERS:D:96A:UN:EGC103'BGM+38G::ZEW+REQEST6957+9'DTM+Z05:0:805'DTM+137:20100930154

值用'分隔。

我可以查詢數據庫並在控制台上顯示該字符串,但是現在我需要提取關鍵字'ORDERS' ,並說它后面有5個字符。 因此輸出應如下所示: ORDERS:D:96A然后我需要提取關鍵字'BGM'及其以下五個字符,因此輸出應如下所示: BGM+38G:

提取所有關鍵字后,結果應以逗號分隔,如下所示:

ORDERS:D:96A,BGM+38G:應將其自動保存到CSV文件中。

我已經嘗試過:

'Lookup for containing KeyWords
                    Dim FoundPosition1 = p_EDI.Contains("ORDERS")
                    Console.WriteLine(FoundPosition1)

這給出了關鍵字的起始位置。

我嘗試將關鍵字“ DTM”的內容全部刪減。 EDI變量保存數據庫中的整個字符串:

Dim FoundPosition2 = EDI
                    FoundPosition2 = Trim(Mid(EDI, InStr(EDI, "DTM")))
                    Console.WriteLine(FoundPosition2)

有人可以幫忙嗎? 先感謝您!

為了說明其中涉及的步驟:

' Find the position where ORDERS is in the string.'
Dim foundPosition = EDI.IndexOf("ORDERS")
' Start at that position and extract ORDERS + 5 characters = 11 characters in total.'
Dim ordersData = EDI.SubString(foundPosition, 11)

' Find the position where BGM is in the string.'
Dim foundPosition2 = EDI.IndexOf("BGM")
' Start at that position and extract BGM + 5 characters = 8 characters in total.'
Dim bgmData = EDI.SubString(foundPosition2, 8)

' Construct the CVS data.'
Dim cvsData = ordersData & "," & bgmData

我在這里沒有我的IDE,但是類似的東西可以工作:

dim EDI as string = "UNH+RAM6957+ORDERS:D:96A:UN:EGC103'BGM+38G::ZEW+REQEST6957+9'DTM+Z05:0:805'DTM+137:20100930154"

dim result as string = KeywordPlus(EDI, "ORDER", 5) + "," _
    + KeywordPlus(EDI, "BGM", 5)

function KeywordPlus(s as string, keyword as string, length as integer) as string
    dim index as integer = s.IndexOf(keyword)
    if index = -1 then return ""
    return s.substring(index, keyword.length + length)
end function

對於我們中間的人,我將代碼放在一起,並從中創建了一個CSV文件。 也許它對別人有幫助......

                 If EDI.Contains("LOC") Then
                    Dim foundPosition1 = EDI.IndexOf("LOC")
                    ' Start at that position and extract ORDERS + 5 characters = 11 characters in total.'
                    Dim locData = EDI.Substring(foundPosition1, 11)
                    'Console.WriteLine(locData)

                    Dim FoundPosition2 = EDI.IndexOf("QTY")
                    Dim qtyData = EDI.Substring(FoundPosition2, 11)
                    'Console.WriteLine(qtyData)

                    ' Construct the CSV data.
                    Dim csvData = locData & "," & qtyData
                    'Console.WriteLine(csvData)

                    ' Creating the CSV File.
                    Dim csvFile As String = My.Application.Info.DirectoryPath & "\Test.csv"
                    Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)

                    outFile.WriteLine(csvData)
                    outFile.Close()
                    Console.WriteLine(My.Computer.FileSystem.ReadAllText(csvFile))
                End IF

玩得開心!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM