从较长的字符串

时间:2016-01-08 20:44:05

标签: c# .net string

我在C#中创建一个使用EConnect将信息导入Great Plains的应用程序。 EConnect拥有它自己的一组例外,如果您正在向应用程序传递错误的信息,那真的很棒。如果抛出EConnectException,我想显示一条错误消息。我的问题是EConnect异常很长,所以我想从中提取一个特定的部分。

异常字符串如下例所示:

  

Microsoft.Dynamics.GP.eConnect.eConnectException:返回了Sql过程错误代码:

     

错误编号= 714存储过程= taPMTransactionInsert错误说明=您无法输入1099值(TEN99AMNT)>而购买金额(PRCHAMNT)减去贸易折扣金额(TRDISAMT)节点标识符参数:taPMTransactionInsert

我真正想要的所有错误消息是错误描述部分。它很容易在错误描述之前删除部分,因为该部分总是相同的长度。错误描述的长度可能会有所不同,因此我无法确定如何提取它。最后我想要的是从错误描述开始并在节点标识符参数之前结束(总是在错误描述之后)

以下是我现在如何捕获异常并切断错误消息的第一部分。 setStatusErrorLogs只是我用来向我的应用程序显示错误的函数。

catch (eConnectException exc)
{
    setStatusErrorLogs((exc.ToString()).Substring(85), xInvoiceNumber + ": ", "CreateInvoice");
}

我是如何提取这个字符串的?

5 个答案:

答案 0 :(得分:4)

使用string.IndexOf方法

var error = exc.ToString();
var startIndex = error.IndexOf("Error Description = ") + "Error Description = ".Length;
var endIndex = error.LastIndexOf("Node Identifier Parameters:");
var desc = error.Substring(startIndex, endIndex - startIndex);
setStatusErrorLogs(desc, xInvoiceNumber + ": ", "CreateInvoice");

这种方法非常容易出错!如果您不确定excetpion的确切格式和内容,那么您应该做检查以获取垃圾数据!

答案 1 :(得分:2)

使用string.IndexOf查找字符串"节点标识符"的索引。然后使用string.SubString获取第85个字符和IndexOf返回的索引之间的所有内容。

答案 2 :(得分:2)

这是一个简单的正则表达式,可以捕获您想要的内容:

.*Error Description(.*)Node Identifier.*

此处使用此代码的代码:

string text = "Error Number = 714 Stored Procedure= taPMTransactionInsert Error Description = You can not enter a 1099 value (TEN99AMNT) > than the Purchase Amount (PRCHAMNT) minus the Trade Discount Amount (TRDISAMT)Node Identifier Parameters: taPMTransactionInsert";
var desc = Regex.Replace(text, ".*Error Description(.*)Node Identifier.*", "$1");
Console.WriteLine(desc);

此正则表达式使用贪婪匹配来确保如果描述包含短语"错误描述"或者"节点标识符",它仍然符合您的预期。

Test out the regex here

答案 3 :(得分:0)

使用Node identifier

查找String.IndexOf()的索引

解决方案就像这样

string exception = exc.ToString();
int nodeIndex = exception.IndexOf("Node identifier");
string errorDescription = exception.Substring(85, nodeIndex);

setStatusErrorLogs(errorDescription, xInvoiceNumber + ": ", "CreateInvoice");

答案 4 :(得分:0)

这可以这样实现:

var lookUpString = "Error Description =";    
var lookUpStringEnd = "Node Identifier";

var stringToLookIn ="Microsoft.Dynamics.GP.eConnect.eConnectException: Sql procedure error codes returned: Error Number = 714 Stored Procedure= taPMTransactionInsert Error Description = You can not enter a 1099 value (TEN99AMNT) > than the Purchase Amount (PRCHAMNT) minus the Trade Discount Amount (TRDISAMT)Node Identifier Parameters: taPMTransactionInsert";

var indexOfLookUpString = stringToLookIn.IndexOf(lookUpString);
var indexOfLookUpStringEnd = stringToLookIn.IndexOf(lookUpStringEnd);

var stringWithLookUpStringIncluded= stringToLookIn.Substring(indexOfLookUpString,indexOfLookUpStringEnd-indexOfLookUpString);

var stringWithoutLookUpStringIncluded = stringToLookIn.Substring(indexOfLookUpString+lookUpString.Length,indexOfLookUpStringEnd -(indexOfLookUpString+lookUpString.Length));

Console.WriteLine(stringWithLookUpStringIncluded);
// output:     Error Description = You can not enter a 1099 value (TEN99AMNT) > than the Purchase Amount (PRCHAMNT) minus the Trade Discount Amount (TRDISAMT)

 Console.WriteLine(stringWithoutLookUpStringIncluded);
 //output:     You can not enter a 1099 value (TEN99AMNT) > than the Purchase Amount (PRCHAMNT) minus the Trade Discount Amount (TRDISAMT)

详细了解String.SubstringString.IndexOf