2

I need to get value which user put in a textbox

Input

Transaction ID : 100000527054518 PNR No. : 6755980353 Train No. / Name : 18615 / KRIYA YOGA EXP Date of Booking : 07-Jun-2016 Class : SLEEPER CLASS Quota : GENERAL Date of Journey : 13-Jun-2016 From : HWH To : RNC Boarding At : HWH Date Of Boarding : 13-Jun-2016 Reservation Up to : RNC Distance : 416 KM Scheduled Departure : 22:10 Scheduled Arrival : 14-Jun-2016 ( 07:05 Hrs ) Total Fare : ? 500.0 & SC : ? 23.0 Adult : 2 & Child : 0 Details of Passengers S.No.Name
Age Gender Concession Status Coach Seat / Berth / WL No Current Status Coach Seat / Berth / WL No ID Type / ID No. 1 AYAN PAL 40 Male CNF S7 49 (LB) CNF S7 49 (LB)

string TransactionID="";
string pnrno="";
string trainno="";
string dateofbooking="";
string class="";
string Quota="";

OUTPUT

TransactionID=100000527054518 ;
pnrno=6755980353;
trainno=18615;
dateofbooking=13-Jun-2016;
class=SLEEPER CLASS;
Quota=GENERAL;

AND If Class Is AC 3 Tier

TransactionID=100000527054518 ;
    pnrno=6755980353;
    trainno=18615;
    dateofbooking=13-Jun-2016;
    class=AC 3 TIER;
    Quota=GENERAL;

Please help I am stuck with this from a time now

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ibibo
  • 167
  • 1
  • 4
  • 21
  • You should use some kind of text parsing API. `Split()` won't be much helpful here. – Rahul Jun 26 '16 at 10:46
  • what should i use then – Ibibo Jun 26 '16 at 10:47
  • 6
    You should rethink your UI concept. Entering all the informations in *one* textbox will force input errors. Use a textbox for each part and the input is easier for the users and the postprocessing is easier for you – Sir Rufo Jun 26 '16 at 10:58
  • Where are you getting the data from? Is it one line or multiple lines? Do you have more than on input of this format in source? Text files parsing must be kept in original format to successfully parse. Been doing this for 40 years and can help. Need exact format of input to help (and multiple samples if possible). – jdweng Jun 26 '16 at 12:12
  • Transaction ID : 100000527054518 PNR No. : 6755980353 Train No. / Name : 18615 / KRIYA YOGA EXP Date of Booking : 07-Jun-2016 Class : SLEEPER CLASS Quota : GENERAL Date of Journey : 13-Jun-2016 From : HWH To : RNC Boarding At : HWH Date Of Boarding : 13-Jun-2016 Reservation Up to : RNC Distance : 416 KM Scheduled Departure : 22:10 Scheduled Arrival : 14-Jun-2016 ( 07:05 Hrs ) Total Fare : ? 500.0 & SC : ? 23.0 Adult : 2 Age Gender Concession Status Coach Seat / Berth / WL No Current Status Coach Seat / Berth / WL No ID Type / ID No. 1 AYAN PAL 40 Male CNF S7 49 (LB) CNF S7 49 (LB) – Ibibo Jun 26 '16 at 13:11
  • 1
    Possible duplicate of [Splitting of words Dynamically in c#](http://stackoverflow.com/questions/38026410/splitting-of-words-dynamically-in-c-sharp) – Sir Rufo Jun 26 '16 at 13:47

3 Answers3

1

This way is working but it's still text dependent one wrong ':' will crush your app,

string source = @"Transaction ID : 100000527054518 PNR No. : 6755980353 Train No. / Name : 18615 / KRIYA YOGA EXP Date of Booking : 07-Jun-2016 Class : SLEEPER CLASS Quota : GENERAL Date of Journey : 13-Jun-2016 From : HWH To : RNC Boarding At : HWH Date Of Boarding : 13-Jun-2016 Reservation Up to : RNC Distance : 416 KM Scheduled Departure : 22:10 Scheduled Arrival : 14-Jun-2016 ( 07:05 Hrs ) Total Fare : ? 500.0 & SC : ? 23.0 Adult : 2 & Child : 0 Details of Passengers S.No.Name
                        Age Gender Concession Status Coach Seat / Berth / WL No Current Status Coach Seat / Berth / WL No ID Type / ID No. 1 AYAN PAL 40 Male CNF S7 49(LB) CNF S7 49(LB)";

        string[] sourceArray = source.Split(':');

        string TransactionID = sourceArray[2].Split(' ')[0];
        string pnrno = "";
        string trainno = "";
        string dateofbooking = "";
        string classStr="";
        string Quota = "";

Option 1

If you have access to the source text you should write it like that:

"Transaction ID : 100000527054518 | PNR No. : 6755980353 | ..."

after that you splitting the text by split('|') after that the next split will be by (':') so what you will get is result[0] = type, result[1] = value

after that in loop :

for(int i = 0 ; i < sourceArray.Count ; i++)
{
   string[] resultArr = sourceArray.Split(':');
   if(resultArr[0].Equals("Transaction ID")) TransactionId = resultArr[1];
   else if ...
}

If you can't edit the source you need to use indexes:

int transactionIndex = source.IndexOf("Transaction ID");
int pnrIndex = source.IndexOf("PNR No.");

and from index take the value from : to : substract the next type

for example the first will be 100000527054518 PNR No. - PNR No. = 100000527054518

Option 2 and i think is the best

use regular expresion

string transactionId;

string source = @"Transaction ID : 100000527054518 PNR No. : 6755980353 Train No. / Name : 18615 / KRIYA YOGA EXP Date of Booking : 07-Jun-2016 Class : SLEEPER CLASS Quota : GENERAL Date of Journey : 13-Jun-2016 From : HWH To : RNC Boarding At : HWH Date Of Boarding : 13-Jun-2016 Reservation Up to : RNC Distance : 416 KM Scheduled Departure : 22:10 Scheduled Arrival : 14-Jun-2016 ( 07:05 Hrs ) Total Fare : ? 500.0 & SC : ? 23.0 Adult : 2 & Child : 0 Details of Passengers S.No.Name
                        Age Gender Concession Status Coach Seat / Berth / WL No Current Status Coach Seat / Berth / WL No ID Type / ID No. 1 AYAN PAL 40 Male CNF S7 49(LB) CNF S7 49(LB)";

Regex transactionRegex = new Regex(@"Transaction ID : [0-9]+ PNR No.");
Match match = transactionRegex.Match(source);

if (match.Success)
{
   transactionId = match.Value.Replace("Transaction ID :", "").Replace("PNR No.", "");
}
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
0

You should use some kind of text parsing API. Split() won't be much helpful here. What you are trying to do is parse the given input and get you the tokens data. Some API's are like (but commercial and not free, though they have 30 days trial)

Rosette Analytics

Actonomy Parser

Rahul
  • 76,197
  • 13
  • 71
  • 125
0

Ok so what i did is class that represent variables you asked to get from the string.

that class has one method that gets string and parsing it with regular expressions.

class Program
    {
        static void Main(string[] args)
        {

            string source = @"Transaction ID : 100000527054518 PNR No. : 6755980353 Train No. / Name : 18615 / KRIYA YOGA EXP Date of Booking : 07-Jun-2016 Class : SLEEPER CLASS : GENERAL Date of Journey : 13-Jun-2016 From : HWH To : RNC Boarding At : HWH Date Of Boarding : 13-Jun-2016 Reservation Up to : RNC Distance : 416 KM Scheduled Departure : 22:10 Scheduled Arrival : 14-Jun-2016 ( 07:05 Hrs ) Total Fare : ? 500.0 & SC : ? 23.0 Adult : 2 & Child : 0 Details of Passengers S.No.Name
                            Age Gender Concession Status Coach Seat / Berth / WL No Current Status Coach Seat / Berth / WL No ID Type / ID No. 1 AYAN PAL 40 Male CNF S7 49(LB) CNF S7 49(LB)";

            MyClass mc1 = new MyClass();

            mc1.getObjectFromString(source);
        }

    }


    class MyClass
    {
        public string TransactionID { get; set; }
        public string pnrno { get; set; }
        public string trainno { get; set; }
        public string dateofbooking { get; set; }
        public string className { get; set; }
        public string Quota { get; set; }

        public void getObjectFromString(string source)
        {

            Regex transactionRegex = new Regex(@"Transaction ID : [0-9]+ PNR No.");
            Regex pnrnoRegex = new Regex(@"PNR No. : [0-9]+ Train No. / Name");
            Regex trainnoRegex = new Regex(@"Train No. / Name : [0-9]*[A-Za-z/ ]* Date of Booking");
            Regex dateofbookingRegex = new Regex(@"Date of Booking : [-0-9a-zA-Z/ ]* Class");
            Regex classNameRegex = new Regex(@"Class : [A-Za-z ]* CLASS");
            Regex QuotaRegex = new Regex(@"CLASS : [A-Za-z ]* Date of Journey");

            Match match = transactionRegex.Match(source);
            if (match.Success)
                this.TransactionID = match.Value.Replace("Transaction ID :", "").Replace("PNR No.", "");

            match = pnrnoRegex.Match(source);
            if (match.Success)
                this.pnrno = match.Value.Replace("PNR No. :", "").Replace("Train No. / Name", "");

            match = trainnoRegex.Match(source);
            if (match.Success)
                this.trainno = new String(match.Value.Replace("Train No. / Name :", "").Replace("Date of Booking", "").ToCharArray().Where(c => Char.IsDigit(c)).ToArray());

            match = dateofbookingRegex.Match(source);
            if (match.Success)
                this.dateofbooking = match.Value.Replace("Date of Booking :", "").Replace("Class", "");

            match = classNameRegex.Match(source);
            if (match.Success)
                this.className = match.Value.Replace("Class :", "").Replace("CLASS", "");

            match = QuotaRegex.Match(source);
            if (match.Success)
                this.Quota = match.Value.Replace("CLASS :", "").Replace("Date of Journey", "");

        }
    }
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
  • if it's working in your app you welcome to accept that answer. – Leon Barkan Jun 27 '16 at 06:44
  • I want it to store in a database but TransactionId And pnrno not apearing on button click SqlCommand cmd = new SqlCommand("insert into testfbhv (TransactionId, pnrno) values('" + TransactionID + "','" + pnrno + "')", con); – Ibibo Jun 27 '16 at 06:53
  • trim the values and check if you get them in the mc1 object if yes you should check whats wrong with db – Leon Barkan Jun 27 '16 at 06:56
  • for better understanding what is regular expression visit : http://www.regular-expressions.info/ – Leon Barkan Jun 27 '16 at 07:03