locked
Split instead of length RRS feed

  • Question

  • Hi,

    I have actually specified the fields with its length. Now I would like to convert from the length to Split using "|" delimeter.

    Kindly help me. Below is my code with length:

    If queryType = "ChargePosting" Then
                gRoomNumber = Trim(Mid(sDoc, sPos, 8))
                sPos = sPos + 8
                gPostingType = Trim(Mid(sDoc, sPos, 1))
                sPos = sPos + 1
                gSalesOutlet = Trim(Mid(sDoc, sPos, 5))
                sPos = sPos + 5
                gPostingAmount = Trim(Mid(sDoc, sPos, 15))
                sPos = sPos + 15
                gDate = Trim(Mid(sDoc, sPos, 6))
                sPos = sPos + 6
                gTime = Trim(Mid(sDoc, sPos, 6))
                sPos = sPos + 6
                gPostingSequenceNumber = Trim(Mid(sDoc, sPos, 4))
                sPos = sPos + 4

    End If

    I have changed the above code to this : But not getting the split result :

    If queryType = "ChargePosting" Then
     Dim a As String
     a = "" & gRoomNumber & "|" & gPostingType & "|" & gSalesOutlet & "|" & gPostingAmount & "|" & gDate & "|" & gTime & "|"   & gPostingSequenceNumber & ""
     Dim b As String() = a.Split(New Char() {"|"c})

    End If

    The Expected Result :

    PA|RN6091|ASOK|DA180206|TI123839|P#1000

    But getting :

    PA|RN|ASOK|DA180206|TI123839|P#

    Tuesday, February 6, 2018 4:46 AM

Answers

  • It took a while, but I think I understand now your problem. 

    You have a file which is not delimited but has fixed positions. You want to make from that a delimited file. 

    Beneath is the code to do that. Be aware, VB follows nows the crazy way of some coders who think that the only thing which should be machine code alike is indexing. First is in .Net done with the number 0 and so on, second is 1 ...... 

    Module Module1
        Private Oldstring As String = "0000001111"
        Private Newstring As String
        Public Sub Main()
            Dim Dstring As New Text.StringBuilder
            Dstring.Append(Oldstring.Substring(0, 8))
            Dstring.Append("|")
            Dstring.Append(Oldstring.Substring(8, 1))
            Dstring.Append("|")
            Dstring.Append(Oldstring.Substring(9, 1))
            Newstring = Dstring.ToString
            Console.WriteLine(Newstring)
            Console.ReadKey()
        End Sub
    End Module


    Success Cor

    • Marked as answer by valliammai26 Wednesday, February 7, 2018 5:53 AM
    Tuesday, February 6, 2018 9:58 AM

All replies

  • The Expected Result :

    PA|RN6091|ASOK|DA180206|TI123839|P#1000

    But getting :

    PA|RN|ASOK|DA180206|TI123839|P#

    The result from that code should be an array of string, but you are showing a single string with delimiters, so it seems the error is in creating the string, not in splitting it.

    You need to insert a breakpoit at the line that creates the string and examine each variable.  It appears that gRoomNumber and gPostingSequenceNumber do not have the values that you think they have.  As this is simple string concatenation then the problem must be in the values, not the code.

    Why is the string enclosed in quotes?

    Tuesday, February 6, 2018 5:15 AM
  • Please provide a complete sample that shows the error. The following is a sample sample.

    Dim a As String = "PA|RN6091|ASOK|DA180206|TI123839|P#1000"
    Dim b As String() = a.Split(New Char() {"|"c})
    Dim aa As String = String.Join(" | ", b)
    Console.WriteLine(aa)



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, February 6, 2018 8:47 AM
  • Hi valliammai26,

    What is sDoc, sPos? Please provide more detailed about character here.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 6, 2018 8:55 AM
  • It took a while, but I think I understand now your problem. 

    You have a file which is not delimited but has fixed positions. You want to make from that a delimited file. 

    Beneath is the code to do that. Be aware, VB follows nows the crazy way of some coders who think that the only thing which should be machine code alike is indexing. First is in .Net done with the number 0 and so on, second is 1 ...... 

    Module Module1
        Private Oldstring As String = "0000001111"
        Private Newstring As String
        Public Sub Main()
            Dim Dstring As New Text.StringBuilder
            Dstring.Append(Oldstring.Substring(0, 8))
            Dstring.Append("|")
            Dstring.Append(Oldstring.Substring(8, 1))
            Dstring.Append("|")
            Dstring.Append(Oldstring.Substring(9, 1))
            Newstring = Dstring.ToString
            Console.WriteLine(Newstring)
            Console.ReadKey()
        End Sub
    End Module


    Success Cor

    • Marked as answer by valliammai26 Wednesday, February 7, 2018 5:53 AM
    Tuesday, February 6, 2018 9:58 AM