Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从较长的字符串中提取字符串_C#_.net_String - Fatal编程技术网

C# 从较长的字符串中提取字符串

C# 从较长的字符串中提取字符串,c#,.net,string,C#,.net,String,我正在用C语言创建一个应用程序,它使用EConnect将信息导入大平原。EConnect有自己的一组异常,如果知道您向应用程序传递了错误的信息,这真是太棒了。如果抛出EConnectException,我想显示一条错误消息。我的问题是EConnect异常非常长,所以我想从中提取一个特定的部分 异常字符串如下例所示: Microsoft.Dynamics.GP.eConnect.eConnectException:返回的Sql过程错误代码: 错误号=714存储过程=taPMTransactionI

我正在用C语言创建一个应用程序,它使用EConnect将信息导入大平原。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");
}

您知道如何提取此字符串吗?

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

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

使用方法

这种方法非常容易出错!如果你不确定例外的确切格式和内容,那么你应该进行检查,不要获取垃圾数据

使用方法


这种方法非常容易出错!如果你不确定例外的确切格式和内容,那么你应该进行检查,不要获取垃圾数据

使用
String.IndexOf()

解决办法是这样的

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

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

使用
String.IndexOf()

解决办法是这样的

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

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

下面是一个简单的正则表达式,它捕获了您想要的内容:

.*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);
这个正则表达式使用贪婪匹配来确保如果描述包含短语“Error description”或“Node Identifier”,它仍然会像您期望的那样匹配


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

.*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);
这个正则表达式使用贪婪匹配来确保如果描述包含短语“Error description”或“Node Identifier”,它仍然会像您期望的那样匹配


这可以通过以下方式实现:

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)

阅读更多关于和

这可以通过以下方式实现:

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)

阅读更多关于和

错误描述是否始终是消息的最后一部分?在节点标识符参数之后还有更多的错误描述…我只是没有发布所有错误,只是为了简单明了,我想知道如何准确地编写我的答案,请参阅dotctor的响应。错误描述是否始终是消息的最后一部分?在节点标识符参数之后还有很多错误…我只是没有将所有错误都发布给simplicityRight,我只是想知道如何准确地编写我的答案,请参阅dotctor的响应。如果描述恰好包含短语,这将无法按预期工作“节点标识符”。OP应改为使用
LastIndexOf
。如果描述恰好包含短语“节点标识符”,则这将无法按预期工作。OP应改为使用
LastIndexOf
。如果描述包含短语“节点标识符”,则此答案也将出现意外行为“.OP应使用
LastIndexOf
或下面的贪婪正则表达式。如果描述中包含短语“节点标识符”,则此答案也将具有意外行为。”。OP应该使用
LastIndexOf
或下面的贪婪正则表达式。