开发者

Get values from string?

开发者 https://www.devze.com 2023-01-23 06:13 出处:网络
How to get the error number and error description fro开发者_如何学Pythonm this string s = \"ERR: 100, out of credit\";

How to get the error number and error description fro开发者_如何学Pythonm this string

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

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


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


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;


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号