开发者

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

I am new to VB.Net 2008. I have a task to resolve, it is regading extracting characters from a long string to the console, the extracted text shall be reformatted and saved into a CSV file. The string comes out of a database.

It looks something like: UNH+RAM6957+ORDERS:D:96A:UN:EGC103'BGM+38G::ZEW+REQEST6957+9'DTM+Z05:0:805'DTM+137:20100930154

The values are seperated by '.

I can query the database and display the string on the console, but now I need to extract the Keyword 'ORDERS' for example, and lets say it's following 5 Characters. So the output should look like: ORDERS:D:96A then I need to extract the keyword 'BGM' and its following five characters so the output should look like: BGM+38G:

After extracting all the keywords, the result should be comma seperated and look like:

ORDERS:D:96A,BGM+38G: it should be saved into a CSV file automatically.

I tried already:

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

Which gives the starting position of the Keyword.

I tried to trim the whole thing around the keyword "DTM". The EDI variable h开发者_JS百科olds the entire string from the Database:

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

Can someone help please? Thank you in advance!


To illustrate the steps involved:

' 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


I don't have my IDE here, but something like this will work:

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


for the interrested people among us, I have put the code together, and created a CSV file out of it. Maybe it can be helpful to others...

                 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

Have fun!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜