How to retreive particular data from a String?
-
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 arraystring 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....
-
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 arraystring 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....
-
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 :)
yes, it is two lines
-
yes, it is two lines
+CMT:"+658208342",,"09/02/08",16:15:22+50"
Test "Message" from coderSo, 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 :)
-
+CMT:"+658208342",,"09/02/08",16:15:22+50"
Test "Message" from coderSo, 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 :)
hi thanks, but i cant split with quotes because the incoming SMS may contain quotes too...! any other method other than split??
-
hi thanks, but i cant split with quotes because the incoming SMS may contain quotes too...! any other method other than split??
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+50and
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 :)
-
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+50and
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 :)
Yea thanks, i will try!
-
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 arraystring 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....
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
-
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
its always 2 lines. is the A,B,C,D,E strings ? can i call these strings later within the method?? Thanks alott. .
-
its always 2 lines. is the A,B,C,D,E strings ? can i call these strings later within the method?? Thanks alott. .
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.
-
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 arraystring 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....
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.
-
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.
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#...
-
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#...
The Values are strings