CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: text formatting

Hybrid View

  1. #1
    Join Date
    Jan 2012
    Posts
    1

    text formatting

    i am trying to edit a csv file from:
    Return(result="success" dev_id="0" time="2011-07-25 16:43:43" id="1" name="persons name" workcode="" status="1" card_src="from_check"

    To:
    "2011-07-25,16:43:43,1,persons name"

    or:
    VarDate VarTime VarID VarName

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: text formatting

    You'll have to be a bit more clear the from csv is not csv the to is.

    If this is to be a function that has those variables and needs to write them as a csv then that is very simple.

    Code:
    OutputString=Var1 & "," & Var2 & "," .........
    If that is not what you are asking then please give more info.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: text formatting

    If you want to extract the actual data from a line like given you'd need a little parser to find the keywords like "result" and then extract the data which comes always in quotes.
    Code:
    Private Function Parse(ByVal Line As String, ByVal KeyWord As String) As String
      Dim p%, p1%, p2%
      p = InStr(Line, KeyWord + "=") 'find the keyword
      If p Then                       'if the keyword plus "=" exists
         p1 = p + Len(KeyWord) + 2   'find starting position of data
         p2 = InStr(p1, Line, """")     'find ending position of data
         Parse = Mid$(Line, p1, p2 - p1) 'extract data
      End If
    
    End Function
    
    Private Sub Command1_Click()
      Dim MyLine$
      MyLine = "Return(result=""success"" dev_id=""0"" time=""2011-07-25 16:43:43"" id=""1"" name=""persons name"" workcode="""" status=""1"" card_src=""from_check"""
      Print Parse(MyLine, "result")
      Print Parse(MyLine, "dev_id")
      Print Parse(MyLine, "time")
    End Sub
    Note, that I had to double all the quotes to put them into a variable. You would presumably read one line like that and have it already imn a variable.
    You call the Parse function for each keyword which then returns the data without anymore quotes.
    Instead of Print the data you would possibly put them in variables like
    VarResult = Parse(MyLine, "result")
    Last edited by WoF; February 1st, 2012 at 08:01 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured