c#中的字符串动态分割

本文关键字:动态 分割 字符串 | 更新日期: 2023-09-27 17:49:17

我需要获取用户在文本框中输入的值

输入

事务ID: 100000527054518 PNR号。: 6755980353次列车/名称:18615/KRIYA YOGA EXP预定日期:2016年6月7日卧铺名额:一般行程日期:2016年6月13日北京时间:北京时间:北京时间:2016年6月13日预定到达:RNC距离:416公里预定出发:22:10预定到达:2016年6月14日(07:05)总票价:?500.0,Sc: ?成人:2 &儿童:0乘客详细信息S.No.Name
年龄性别优惠价身份经济舱座位/铺位/WL无现时客舱座位/铺位/客位号码类型/客位号码1号PAL 40男CNF S7 49 (LB) CNF S7 49 (LB)

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

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

AND If Class Is ac3 Tier

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

请帮助我从一段时间现在卡住了

c#中的字符串动态分割

这种方式是有效的,但它仍然依赖于文本,一个错误的':'会破坏你的应用程序,

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 = "";

选项1

如果你可以访问源文本,你应该这样写:

"事务ID: 100000527054518 | PNR号。: 6755980353 |…"

之后用split('|')分割文本然后用(':')分割文本所以你会得到result[0] = type, result[1] = value

in循环后:

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

如果你不能编辑源代码,你需要使用索引:

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

和from index取值from: to:减去下一个类型

例如,

第一个将是100000527054518 PNR号。-申请编号= 100000527054518

选项2,我认为是最好的

使用正则表达式
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.", "");
}

您应该使用某种文本解析API。Split()在这里没有多大帮助。您要做的是解析给定的输入并获取令牌数据。有些API是这样的(但是是商业的,不是免费的,尽管他们有30天的试用期)

玫瑰分析

Actonomy解析器

我所做的是一个类,它代表了你要求从字符串中获取的变量。

这个类有一个方法获取字符串并用正则表达式解析它。

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", "");
        }
    }