[英]How can I refactor this C# code using Split()?
我如何重构它,这样就不必将numberOfItems声明为变量?
//method: gets the text in a string in front of a marker, if marker is not there, then return empty string
//example: GetTextAfterMarker("documents/jan/letter043.doc","/") returns "letter043.doc"
//example: GetTextAfterMarker("letter043.doc","/") returns ""
//example: GetTextAfterMarker("letter043.doc",".") returns "doc"
public static string GetTextAfterMarker(string line, string marker)
{
int numberOfItems = line.Split(new string[] { marker }, StringSplitOptions.None).Count();
string result = line.Split(new string[] { marker }, StringSplitOptions.None)[numberOfItems-1];
return line.Equals(result) ? string.Empty : result;
}
var index = line.LastIndexOf (marker) ;
return index < 0 ? string.Empty : line.Substring (index + marker.Length) ;
如果您使用的是3.5,则与Linq无关紧要:
public static string GetTextAfterMarker2(string line, string marker)
{
string result = line.Split(new string[] { marker }, StringSplitOptions.None).Last();
return line.Equals(result) ? string.Empty : result;
}
我会这样
public static string GetTextAfterMarker(string line, string marker)
{
int pos = line.LastIndexOf(marker);
if (pos < 0)
return string.Empty;
else
return line.Substring(pos + marker.Length);
}
无需调用split,无需创建项目数组,基本上,您只是将其丢弃。
更快,资源更少。
如果我正确地阅读了您的代码,那么您正在尝试获取最后一个实例?
string[] splits = line.Split(new string[] { marker }, StringSplitOptions.None);
return (splits.Length == 1) ? string.Empty : splits[splits.Length-1];
尽管我不知道您是否出于好奇而问过这个问题,但我建议您不要担心创建辅助变量,如果这样做会使您的代码更具可读性。 使用这些额外变量几乎不会降低代码性能。
public static string GetTextAfterMarker(string line, string marker)
{
string result = line.Split(new string[] { marker }, StringSplitOptions.None)[line.Split(new string[] { marker }, StringSplitOptions.None).Count()-1];
return line.Equals(result) ? string.Empty : result;
}
尽管您为什么想这样做,但至少可以这样说。
也许:
public static string GetTextAfterMarker(string line, string marker)
{
string[] tmp = line.Split(new string[] { marker }, StringSplitOptions.None);
string result = tmp[tmp.Length-1];
return line.Equals(result) ? string.Empty : result;
}
或者,您甚至可以考虑改用.IndexOf
和.Substring
,如下所示(未经测试):
public static string GetTextAfterMarker(string line, string marker)
{
int index = line.LastIndexOf(marker);
return index < 0 ? string.Empty : line.Substring(index + marker.Length);
}
关于什么:
public static string GetTextAfterMarker(string line, string marker)
{
var items = line.Split(new[] {marker}, StringSplitOptions.None);
string result = items[items.Length-1];
return line.Equals(result) ? string.Empty : result;
}
您可以始终使用正则表达式而不是split:
public static string GetTextAfterMarker(string line, string marker)
{
if (String.IsNullOrEmpty(line))
throw new ArgumentException("line is null or empty.", "line");
if (String.IsNullOrEmpty(marker))
throw new ArgumentException("marker is null or empty.", "marker");
string EscapedMarker = Regex.Escape(marker);
return Regex.Match(line, EscapedMarker + "([^" + EscapedMarker + "]+)").Groups[1].Value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.