C# code to extract particular data from a string [Resolved]

Posted by Sudhakar_A under Windows Forms on 1/28/2014 | Points: 10 | Views : 934 | Status : [Member] | Replies : 2
The Data below is ma output string:-
-----------------------------------------------------------------------------
$OBD_070001

[+CSQ: 22,0]
data while 1 in
data while 1 out
ok while in:25
ok while out
GSM Signal strengless is 22.
$INFO=352964054830741,RFM-5200,2.3.65.67,352964054830741,404201530037439,8991200015300374392,139,42,0,1,22,1,0

Ok while in
GSM signal
---------------------------------------------------------------------------
I want to extract data "$INFO=352964054830741,RFM-5200,2.3.65.67,352964054830741,404201530037439,8991200015300374392,139,42,0,1,22,1,0
" from the above string to new string which is ma actually Data.


Pls help me to sort out......Thnk's in advance




Responses

Posted by: kgovindarao523-21772 on: 1/28/2014 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
Hi,
Here is C# code.
Revert back if any concerns, else Please Mark as Answer

public void myFunction()
{
string s = "$OBD_070001[+CSQ: 22,0]data while 1 indata while 1 outok while in:25ok while outGSM Signal strengless is 22.$INFO=352964054830741,RFM-5200,2.3.65.67,352964054830741,404201530037439,8991200015300374392,139,42,0,1,22,1,0 ";
int n = GetNthIndex(s, '$', 2);
string s1= s.Substring(n);
}
public int GetNthIndex(string s, char t, int n)
{
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}



Thank you,
Govind

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudhakar_A on: 1/28/2014 [Member] Starter | Points: 25

Up
0
Down
Thnk's @ K.Govindarao

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response