Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to retreive particular data from a String?

How to retreive particular data from a String?

Scheduled Pinned Locked Moved C#
data-structuresregextutorialquestion
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Aghosh Babu wrote:

    +CMT:"+658208342",,"09/02/08",16:15:22+50" Test Message from coder

    Are they on two separate lines? In other words, is that an '\n' character I see within the string?

    I are troll :)

    A Offline
    A Offline
    Aghosh Babu
    wrote on last edited by
    #3

    yes, it is two lines

    L 1 Reply Last reply
    0
    • A Aghosh Babu

      yes, it is two lines

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      +CMT:"+658208342",,"09/02/08",16:15:22+50"
      Test "Message" from coder

      So, this would fail, since there are quotes on the second line? Would it help if you split the string based on the NewLine first? Something like this;

      string[] Message = s.Split('\n');

      This will put the first line in Message[0], and the text-message in Message[1]. You can then safely split your values on the quotes like;

      string[] values = Message[0].Split(new char[] { '"' });

      HTH :)

      I are troll :)

      A 1 Reply Last reply
      0
      • L Lost User

        +CMT:"+658208342",,"09/02/08",16:15:22+50"
        Test "Message" from coder

        So, this would fail, since there are quotes on the second line? Would it help if you split the string based on the NewLine first? Something like this;

        string[] Message = s.Split('\n');

        This will put the first line in Message[0], and the text-message in Message[1]. You can then safely split your values on the quotes like;

        string[] values = Message[0].Split(new char[] { '"' });

        HTH :)

        I are troll :)

        A Offline
        A Offline
        Aghosh Babu
        wrote on last edited by
        #5

        hi thanks, but i cant split with quotes because the incoming SMS may contain quotes too...! any other method other than split??

        L 1 Reply Last reply
        0
        • A Aghosh Babu

          hi thanks, but i cant split with quotes because the incoming SMS may contain quotes too...! any other method other than split??

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          The incoming SMS will contain quotes on the second line? If so, there shouldn't be any problem splitting them once you have separated the first and the second line? In other words, Split only on "line 1", without touching line 2:

          +CMT:
          +658208342
          09/02/08
          ,16:15:22+50

          and

          Test "Message' from coder

          There are lots of methods to manipulate strings, including, but not limited to, regular expressions, the internal string-routines (Substring, IndexOfAny), parsers..

          I are troll :)

          A 1 Reply Last reply
          0
          • L Lost User

            The incoming SMS will contain quotes on the second line? If so, there shouldn't be any problem splitting them once you have separated the first and the second line? In other words, Split only on "line 1", without touching line 2:

            +CMT:
            +658208342
            09/02/08
            ,16:15:22+50

            and

            Test "Message' from coder

            There are lots of methods to manipulate strings, including, but not limited to, regular expressions, the internal string-routines (Substring, IndexOfAny), parsers..

            I are troll :)

            A Offline
            A Offline
            Aghosh Babu
            wrote on last edited by
            #7

            Yea thanks, i will try!

            1 Reply Last reply
            0
            • A Aghosh Babu

              I am developing an sms application, whenever the application receive an sms it is assigned to a string. the string value is something like this: +CMT:"+658208342",,"09/02/08",16:15:22+50" Test Message from coder String text = (above code lines) i have to take the Mobile number, datetime , and text as 3 different string from the above string. Now what i had is that i use split whenever a string have " and then add it to an array

              string s = text.Replace(",,", "");
              string[] values = s.Split(new char[] { '"' });
              //MessageBox.Show(Convert.ToString(values[3]));
              string incoming_msg_number = values[1];
              Thread.Sleep(50);
              string msg_date_time = values[3];
              Thread.Sleep(50);
              string msg_text = values[4].Trim();
              MessageBox.Show("Number :" + incoming_msg_number + " \r\nDate & Time: " + msg_date_time +
              "\r\nMessage :" + msg_text);*

              It DIDNT work because there are chances of quaotes within the incoming message :^) Will regex can do something with this?? if so how?? thanks....

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #8

              I would use a Regular Expression to split the string. Is it always two lines per message? Or sometimes more? This

              System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex
              (
              "(?'A'[^:]*):\"(?'B'[^\"]*)\",,\"(?'C'[^\"]*)\",(?'D'[^\"]*)\"\n(?'E'[^\n]*)\n"
              ,
              System.Text.RegularExpressions.RegexOptions.Compiled |
              System.Text.RegularExpressions.RegexOptions.Singleline |
              System.Text.RegularExpressions.RegexOptions.CultureInvariant
              ) ;

              is giving me:

              A >+CMT<
              B >+658208342<
              C >09/02/08<
              D >16:15:22+50<
              E >Test Message from coder<

              Did you leave out a quote in your data?

              modified on Sunday, February 8, 2009 9:21 AM

              A 1 Reply Last reply
              0
              • P PIEBALDconsult

                I would use a Regular Expression to split the string. Is it always two lines per message? Or sometimes more? This

                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex
                (
                "(?'A'[^:]*):\"(?'B'[^\"]*)\",,\"(?'C'[^\"]*)\",(?'D'[^\"]*)\"\n(?'E'[^\n]*)\n"
                ,
                System.Text.RegularExpressions.RegexOptions.Compiled |
                System.Text.RegularExpressions.RegexOptions.Singleline |
                System.Text.RegularExpressions.RegexOptions.CultureInvariant
                ) ;

                is giving me:

                A >+CMT<
                B >+658208342<
                C >09/02/08<
                D >16:15:22+50<
                E >Test Message from coder<

                Did you leave out a quote in your data?

                modified on Sunday, February 8, 2009 9:21 AM

                A Offline
                A Offline
                Aghosh Babu
                wrote on last edited by
                #9

                its always 2 lines. is the A,B,C,D,E strings ? can i call these strings later within the method?? Thanks alott. .

                P 1 Reply Last reply
                0
                • A Aghosh Babu

                  its always 2 lines. is the A,B,C,D,E strings ? can i call these strings later within the method?? Thanks alott. .

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #10

                  Those are groups, and yes you can access then by name. If you have a class/struct to hold the data

                  MyObject objX ;

                  foreach ( System.Text.RegularExpressions.Match mat in reg.Matches ( text ) )
                  {
                  objX = new MyObject
                  (
                  mat.Groups [ "A" ].Value
                  ,
                  mat.Groups [ "B" ].Value
                  ,
                  mat.Groups [ "C" ].Value
                  ,
                  mat.Groups [ "D" ].Value
                  ,
                  mat.Groups [ "E" ].Value
                  ) ;
                  }

                  And parse the values in the constructor. That's just one way.

                  A 1 Reply Last reply
                  0
                  • A Aghosh Babu

                    I am developing an sms application, whenever the application receive an sms it is assigned to a string. the string value is something like this: +CMT:"+658208342",,"09/02/08",16:15:22+50" Test Message from coder String text = (above code lines) i have to take the Mobile number, datetime , and text as 3 different string from the above string. Now what i had is that i use split whenever a string have " and then add it to an array

                    string s = text.Replace(",,", "");
                    string[] values = s.Split(new char[] { '"' });
                    //MessageBox.Show(Convert.ToString(values[3]));
                    string incoming_msg_number = values[1];
                    Thread.Sleep(50);
                    string msg_date_time = values[3];
                    Thread.Sleep(50);
                    string msg_text = values[4].Trim();
                    MessageBox.Show("Number :" + incoming_msg_number + " \r\nDate & Time: " + msg_date_time +
                    "\r\nMessage :" + msg_text);*

                    It DIDNT work because there are chances of quaotes within the incoming message :^) Will regex can do something with this?? if so how?? thanks....

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #11

                    You can limit the number of items that the Split returns, so that you get everything after the fifth quotation mark in the sixth item: string[] values = s.Split(new char[] { '"' }, 6);

                    Despite everything, the person most likely to be fooling you next is yourself.

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Those are groups, and yes you can access then by name. If you have a class/struct to hold the data

                      MyObject objX ;

                      foreach ( System.Text.RegularExpressions.Match mat in reg.Matches ( text ) )
                      {
                      objX = new MyObject
                      (
                      mat.Groups [ "A" ].Value
                      ,
                      mat.Groups [ "B" ].Value
                      ,
                      mat.Groups [ "C" ].Value
                      ,
                      mat.Groups [ "D" ].Value
                      ,
                      mat.Groups [ "E" ].Value
                      ) ;
                      }

                      And parse the values in the constructor. That's just one way.

                      A Offline
                      A Offline
                      Aghosh Babu
                      wrote on last edited by
                      #12

                      if u dont mind can u pls explain me how i combine those codes?? where should i add the string?? how i retreive the strings from the regex and display it in a msg box?? thanks a lott... i am a beginer in C#...

                      P 1 Reply Last reply
                      0
                      • A Aghosh Babu

                        if u dont mind can u pls explain me how i combine those codes?? where should i add the string?? how i retreive the strings from the regex and display it in a msg box?? thanks a lott... i am a beginer in C#...

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #13

                        The Values are strings

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups