Kristen
Kristen

Reputation: 453

Extracting string from a longer string

I'm creating an app in C# that uses EConnect to import information into Great Plains. EConnect has it own set of exceptions which are really great to know if you are passing the app the wrong information. I want to display an error message if an EConnectException is thrown. My problem is the EConnect exception is very long so I want to extract a particular part from it.

The exception string look like the following example:

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

All I really want for my error messages is the Error Description part. Its easy enough to cut out the part before Error Description because that portion is always the same length. The Error Description can vary in length so thats where I'm having trouble figuring out how to extract it. In the end what I would want it the starting at Error Description and ending before Node Identifier Parameters (which always comes after the Error Description)

Here is how I catch my exception right now and cut off the first part of the error message. setStatusErrorLogs is just a function I use to display my errors to my app.

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

Any ideas how I can extract this string?

Upvotes: 1

Views: 206

Answers (5)

Hamid Pourjam
Hamid Pourjam

Reputation: 20764

Use string.IndexOf method

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

This approach is very error prone! If you are not sure about the exact format and content of the excetpion then you should do checks not to get junk data!

Upvotes: 4

Tom B.
Tom B.

Reputation: 2962

This can be achieved like this:

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)

Read more about String.Substring and String.IndexOf

Upvotes: 0

user2023861
user2023861

Reputation: 8208

Here's a simple Regex that captures what you want:

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

Here's code that uses this:

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);

This regex uses greedy matching to ensure that if the description contains the phrase "Error Description" or "Node Identifier", it'll still match as you expect.

Test out the regex here

Upvotes: 2

Jhon
Jhon

Reputation: 582

Find the index of Node identifier with String.IndexOf()

Solution would be like this

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

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

Upvotes: 0

Bradley Uffner
Bradley Uffner

Reputation: 16991

Use string.IndexOf to find the index of the string "Node Identifier". then use string.SubString to get everything between the 85th character and the index returned by IndexOf.

Upvotes: 2

Related Questions