If I have a string like this
@"400 ERROR The second argument must be larger than the first."
How do I extract the part that says "The second argument must be larger than the first."
?
If I have a string like this
@"400 ERROR The second argument must be larger than the first."
How do I extract the part that says "The second argument must be larger than the first."
?
string error = @"400 ERROR The second argument must be larger than the first.";
var ind1 = error.IndexOf(' ');
var ind2 = error.IndexOf(' ', ind1 + 1);
var substring = error.Substring(ind2);
How ever this may potentially fail in various cases. for example having multiple spaces behind each other. using this method may be error-prone.
Regex is better option.
string error = @"400 ERROR The second argument must be larger than the first.";
Regex regex = new Regex("^\\d+ *(ERROR|WARNING) *(?<Message>.*)$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
var message = regex.Match(error).Groups["Message"].ToString();
You can add as many as patterns you want in your first capture.like this (ERROR|WARNING|HINT|etc)
Try this:
var source = @"400 ERROR The second argument must be larger than the first.";
var result = String.Join(" ", source.Split(' ').Skip(2));
That gives me the result you're looking for.
var result = String.Join(" ",error.split(' ').Skip(2))
Or this
var output = Regex.Replace(ErrorText,@"\d+?\s\w+","");