0

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))
                    {
                        //
                    }
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
Adi
  • 1,395
  • 11
  • 37
  • 61

5 Answers5

3

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;
        }

Zeeshan Ajmal
  • 880
  • 1
  • 16
  • 29
1

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
    }
default
  • 11,485
  • 9
  • 66
  • 102
  • thank u sir ..one small query i have to take "Total" line also into result ..then how to do it? – Adi Feb 18 '14 at 12:23
  • [IndexOf](http://msdn.microsoft.com/en-us/library/7cct0x33%28v=vs.110%29.aspx) takes a startposition, which I guess you could use to find the next `newline` after the endPosition index. – default Feb 18 '14 at 14:49
0

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

  • 2
    Except he wants to read all lines in between `1 . 1 To Airtel Mobile` and `Total 25:28 9.30`. – Prix Feb 18 '14 at 11:52
  • @Prix yes I know thats why I mentioned he needs some more logics to handle those line. And for call details solution is already given. –  Feb 18 '14 at 11:54
  • yes i want to read all lines in between "1 . 1 To Airtel Mobile" and this "Total 25:28 9.30" not to split each line what u have given solution to.. – Adi Feb 18 '14 at 11:54
0

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.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
0

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"));
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459