[英]get string after second space
如果我有這樣的字符串
@"400 ERROR The second argument must be larger than the first."
如何提取"The second argument must be larger than the first."
的部分"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);
在各種情況下,這可能會失敗。 例如彼此之間有多個空間。 使用此方法可能容易出錯。
正則表達式是更好的選擇。
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();
您可以在第一次捕獲中添加任意數量的模式。例如(ERROR|WARNING|HINT|etc)
嘗試這個:
var source = @"400 ERROR The second argument must be larger than the first.";
var result = String.Join(" ", source.Split(' ').Skip(2));
這給了我您想要的結果。
var result = String.Join(" ",error.split(' ').Skip(2))
或這個
var output = Regex.Replace(ErrorText,@"\d+?\s\w+","");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.