考虑下面的输入字符串。
var inputString = "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp?" + Environment.NewLine;
inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
inputString += "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp? " + Environment.NewLine;
inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
inputString += "Config: PartnerServicesTestBase.dll PaymentSOAUrl for Paid: https://172.31.26.38/Payment.asp? " + Environment.NewLine;
inputString += "Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
inputString += "Config: PartnerServicesTestBase.dll PaymentSOAUrl for Paid: https://172.31.26.38/Payment.asp? " + Environment.NewLine;
inputString += "Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>" + Environment.NewLine;
inputString += "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp? " + Environment.NewLine;
inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
让我们观察“Exceptions”的字符串内容(给出的示例中共有 5 个)。
第一
Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:
第二个
Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:
第三个
Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:
第四人
Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:
第五人
Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:
如果我们仔细看,第一种情况和第二种情况是相同的。第三和第四也是如此。第 5 个与第 1/2 个相同。
我需要做的是,如果第一个和第二个或连续的“异常”文本是相同的,那么留下第一个,其他的将被替换为“异常:--DO--”。如果找到一个相同的字符串但不是连续的,那么它将按原样显示。
此后,输出将是(原始字符串的所有内容都将保持不变,只有匹配的异常将被更新,如下所示)
第一
Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:
SecondOne(因为它与 Firstone 相同并且是连续的)
Exception: --DO--
ThirdOne(因为是新的)
Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:
FourthOne(因为它与 Thirdone 相同并且是连续的)
Exception: --DO--
FifthOne(虽然它与第一个/第二个相同,但不是连续的)
Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:
我做了这个程序。首先,我根据索引(开始和结束)获取异常的内容,然后将值存储在集合中。然后使用循环检查第一个和第二个的内容并进行所需的更新。
但我相信还有很多比这更好的解决方案(可能是正则表达式、linq、lambda组合。)
需要帮助以有效的方式解决此问题。
谢谢