Reputation: 1455
I have a string which contains lots of texts in which there is substring like ..
1 . 1 To Airtel Mobile
1 03/MAR/2013 16:06:31 9845070641 05:44 1.80 **
2 04/MAR/2013 10:00:29 9845096416 00:14 0.30 **
3 04/MAR/2013 20:02:35 9845096416 08:12 2.70 **
4 06/MAR/2013 21:20:03 9632176702 00:37 0.30 **
5 09/MAR/2013 11:40:45 9845444042 01:29 0.60 **
6 11/MAR/2013 18:59:08 9900054971 01:14 0.60 **
7 12/MAR/2013 13:43:01 9686568009 03:57 1.20 **
8 13/MAR/2013 17:38:18 7760995045 00:48 0.30 **
9 21/MAR/2013 09:26:20 9845444043 02:47 0.90 **
10 21/MAR/2013 11:02:39 9845444043 00:15 0.30 **
11 22/MAR/2013 18:00:00 9845096416 00:11 0.30 **
Total 25:28 9.30
Now as per my requirement i have to read the lines after this substring "1 . 1 To Airtel Mobile" to this substring "Total 25:28 9.30".Here is my code in c# which will check if "1 . 1 To Airtel Mobile" is present in the string.Now as per my requirement how to read the lines up to the specified substring i.e "Total 25:28 9.30". Here is my code in C#..
if(currentText.Contains("1 . 1 To Airtel Mobile"))
{
using (StringReader reader = new StringReader(currentText))
{
//
}
Upvotes: 0
Views: 394
Reputation: 880
I have created a sample project and test below code with your given input and it gave the result you required.
string strStartString = "1 . 1 To Airtel Mobile";
string strEndString = "Total 25:28 9.30";
if (strRawData.StartsWith(strStartString) && strRawData.EndsWith(strEndString))
{
int startIndex = strRawData.IndexOf("1 . 1 To Airtel Mobile");
int endIndex = strRawData.IndexOf("Total 25:28 9.30");
int whereReadingStarts = strStartString.Length;
int whereReadingStops = endIndex - whereReadingStarts;
string strDesiredOutput = strRawData.Substring(whereReadingStarts, whereReadingStops);
textBox1.Text = strDesiredOutput;
}
Upvotes: 3
Reputation: 236268
You can use SkipWhile to skip all lines until To Airtel Mobile
header. Then skip this header. And then with TakeWhile take next lines until total line:
var options = StringSplitOptions.RemoveEmptyEntries;
var lines = text.Split(new [] { Environment.NewLine }, options)
.SkipWhile(s => !s.Contains("1 . 1 To Airtel Mobile"))
.Skip(1)
.TakeWhile(s => !s.Contains("Total"));
Upvotes: 0
Reputation: 11635
I guess this should work for you
if (currentText.Contains("1 . 1 To Airtel Mobile") && currentText.Contains("Total"))
{
int startPosition = currentText.IndexOf("1 . 1 To Airtel Mobile");
int endPosition = currentText.IndexOf("Total");
string result = currentText.Substring(startPosition, endPosition-startPosition);
// result will contain everything from and up to the Total line
}
Upvotes: 1
Reputation: 24470
Assuming you already have all the needed text you could do something like:
var lines = str.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
Now just skip first and last item in lines
.
Upvotes: 0
Reputation:
You can use string split function here for handling call details.
var txt ="1 03/MAR/2013 16:06:31 9845070641 05:44 1.80 **";
string [] split = txt.Split(new Char [] {' '});
// now you have
split[0] = 1;
split[1]="03/MAR/2013";
split[2]="16:06:31";
split[3]="9845070641";
split[4]="05:44";
split[5]="1.80";
split[6]="*";
Do whatever you want with your data. You need some more logic here to handle lines other than having call details
Upvotes: 0