views:

54

answers:

4

How to get the error number and error description from this string

s = "ERR: 100, out of credit";

error should equal "100" error description should equal "out of credit"

A: 

split the string with ',' and the 1st part of the splitted array will be error and 2nd element will be the error text. This wil work if the format of string will not change

saurabh
+3  A: 

If the format is always ERR: "code", "desc" you could do some regex on it pretty easily. In C#:

string s = "ERR: 100, out of credit";

Match m = Regex.Match(s, "ERR: ([^,]), (.)");

string error = m.Groups[1].Value; string description = m.Groups[2].Value;

RTigger
@RTigger. Have to go with BrokenGlass on this one. Regexes are inefficient compared to splits.
Jordan
+3  A: 
string message = "ERR: 100, out of credit";
string[] parts = message.Split(new char[] { ',' });
string[] error = parts[0].Split(new char[] { ':' });

string errorNumber = error[1].Trim();
string errorDescription = parts[1].Trim();
BrokenGlass
A: 

string s = "ERR: 100, out of credit";

Match m = Regex.Match(s, "[ERR:\s*]\d+"); Match n = Regex.Match(s, "(?<=ERR: \d+, ).+"); string errorno = m.value string errordesc = n.value

hope this will useful

Kumaran T