string splitting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xyz

    string splitting

    I have a string
    16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 tcp 168

    for example lets say for the above string
    16:23:18.659343 -- time
    131.188.37.230 -- srcaddress
    22 --srcport
    131.188.37.59 --destaddress
    1398 --destport
    tcp --protocol
    168 --size
    i need to split this string such that i need to get all these
    parameters....

    thank you for any help
  • Ian Collins

    #2
    Re: string splitting

    xyz wrote:
    I have a string
    16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 tcp 168
    >
    for example lets say for the above string
    16:23:18.659343 -- time
    131.188.37.230 -- srcaddress
    22 --srcport
    131.188.37.59 --destaddress
    1398 --destport
    tcp --protocol
    168 --size
    i need to split this string such that i need to get all these
    parameters....
    >
    So, what have you tried?

    Whether or not the field widths are fixed will make a significant impact
    on how you split the sting.

    --
    Ian Collins.

    Comment

    • xyz

      #3
      Re: string splitting

      On Apr 29, 12:43 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      xyz wrote:
      I have a string
      16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 tcp 168
      >
      for example lets say for the above string
      16:23:18.659343 -- time
      131.188.37.230   -- srcaddress
      22                        --srcport
      131.188.37.59    --destaddress
      1398                  --destport
      tcp                    --protocol
      168                  --size
      i need to split this string such that i need to get all these
      parameters....
      >
      So, what have you tried?
      >
      Whether or not the field widths are fixed will make a significant impact
      on how you split the sting.
      >
      --
      Ian Collins.

      the field widths are not fixed..i have some times four/three digits
      srcport ..so i cant do it with substr function
      i am not getting an idea how to split it..
      thank you

      Comment

      • xyz

        #4
        Re: string splitting

        On Apr 29, 12:52 pm, xyz <lavanyaredd... @gmail.comwrote :
        On Apr 29, 12:43 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        >
        >
        >
        xyz wrote:
        I have astring
        16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 tcp 168
        >
        for example lets say for the abovestring
        16:23:18.659343 -- time
        131.188.37.230   -- srcaddress
        22                        --srcport
        131.188.37.59    --destaddress
        1398                  --destport
        tcp                    --protocol
        168                  --size
        i need to split thisstringsuch that i need to get all these
        parameters....
        >
        So, what have you tried?
        >
        Whether or not the field widths are fixed will make a significant impact
        on how you split the sting.
        >
        --
        Ian Collins.
        >
        the field widths are not fixed..i have some times four/three digits
        srcport ..so i cant do it with substr function...i need this in c++
        i am not getting an idea how to split it..
        thank you

        Comment

        • fnegroni

          #5
          Re: string splitting

          There is a nasty function called strtok().
          But you can most probably write your own implementation if you
          understand what it is that you want to achieve:
          1) skip all separators, no matter how many
          2) null terminate all tokens (words, parameters)
          3) remember the offset of each token.

          Comment

          • xyz

            #6
            Re: string splitting

            On Apr 29, 1:26 pm, fnegroni <f.e.negr...@go oglemail.comwro te:
            There is a nasty function called strtok().
            But you can most probably write your own implementation if you
            understand what it is that you want to achieve:
            1) skip all separators, no matter how many
            2) null terminate all tokens (words, parameters)
            3) remember the offset of each token.
            I have a string
            16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 tcp 168
            i have so many of such lines
            for example lets say for the above string
            16:23:18.659343 -- time
            131.188.37.230 -- srcaddress
            22 --srcport
            131.188.37.59 --destaddress
            1398 --destport
            tcp --protocol
            168 --size
            i need to split this string such that i need to get all these
            parameters....

            thank you for any help

            Comment

            • santosh

              #7
              Re: string splitting

              xyz wrote:
              On Apr 29, 1:26 pm, fnegroni <f.e.negr...@go oglemail.comwro te:
              >There is a nasty function called strtok().
              >But you can most probably write your own implementation if you
              >understand what it is that you want to achieve:
              >1) skip all separators, no matter how many
              >2) null terminate all tokens (words, parameters)
              >3) remember the offset of each token.
              >
              I have a string
              16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 tcp 168
              i have so many of such lines
              for example lets say for the above string
              16:23:18.659343 -- time
              131.188.37.230 -- srcaddress
              22 --srcport
              131.188.37.59 --destaddress
              1398 --destport
              tcp --protocol
              168 --size
              i need to split this string such that i need to get all these
              parameters....
              >
              thank you for any help
              Well, look closely at the string you have presented and you will see
              that all unrelated elements are separated by a space. First you have
              the time field with three components, each separated by the ':'
              character. The total field length is 15 characters. Then comes the
              combination of source address and port, this time the components are
              separated by a '.' character and the last component specifies the
              source port. The destination address and port field follows the same
              organisation. The comes the protocol field which is usually TCP or
              perhaps UDP. The remaining component specifies the payload size.

              So to split this string to get the components in the manner you
              indicate, splice the string at each whitespace character. Then for
              second and third substrings that you so get split of all characters
              that follow the fourth '.' character.

              Note that the above method is totally hackish and fragile. If you want a
              robust solution then you must study the protocol involved in detail and
              either construct your own parser for it or use one of the many free
              ones available on the Net.

              Now try doing it and if the code is in C you can post here for further
              help and comments. If you attempt it in C++ then post to comp.lang.c++.

              Comment

              Working...