How can I refactor this so that numberOfItems doesn't have to be declared as a variable?
我怎样才能重构这一点,以便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;
}
8 个解决方案
#1
If you're using 3.5 this is trivial with Linq:
如果你使用的是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;
}
#2
var index = line.LastIndexOf (marker) ;
return index < 0 ? string.Empty : line.Substring (index + marker.Length) ;
#3
If I'm reading your code correctly, you're trying to get the last instance?
如果我正确地读取您的代码,您是否正在尝试获取最后一个实例?
string[] splits = line.Split(new string[] { marker }, StringSplitOptions.None);
return (splits.Length == 1) ? string.Empty : splits[splits.Length-1];
#4
I'd do it this way
我这样做
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);
}
No need to call split, no need to create an array of items, which basically you're just going to throw away.
不需要调用split,也不需要创建一个项目数组,基本上你只是要扔掉。
Much faster and less resource heavy.
更快,更少资源。
#5
Altough I don't know wether you asked this out of simple curiosity, I would recommend you to not being afraid of creating auxiliary variables if that makes your code more readable. You will rarely degrade your code performance for using these extra variables.
尽管我不知道你是出于简单的好奇心问这个问题,我建议你不要害怕创建辅助变量,如果这样可以让你的代码更具可读性。使用这些额外变量时,您很少会降低代码性能。
#6
What about:
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;
}
#7
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;
}
Why you'd want to do that though is beyond me... It's messy, to say the least.
为什么你想要做到这一点虽然超出了我...至少可以说它很混乱。
Perhaps:
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;
}
Or you might even consider using .IndexOf
and .Substring
instead, something like this (untested):
或者您甚至可以考虑使用.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);
}
#8
You could always use regular expressions instead of 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;
}
#1
If you're using 3.5 this is trivial with Linq:
如果你使用的是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;
}
#2
var index = line.LastIndexOf (marker) ;
return index < 0 ? string.Empty : line.Substring (index + marker.Length) ;
#3
If I'm reading your code correctly, you're trying to get the last instance?
如果我正确地读取您的代码,您是否正在尝试获取最后一个实例?
string[] splits = line.Split(new string[] { marker }, StringSplitOptions.None);
return (splits.Length == 1) ? string.Empty : splits[splits.Length-1];
#4
I'd do it this way
我这样做
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);
}
No need to call split, no need to create an array of items, which basically you're just going to throw away.
不需要调用split,也不需要创建一个项目数组,基本上你只是要扔掉。
Much faster and less resource heavy.
更快,更少资源。
#5
Altough I don't know wether you asked this out of simple curiosity, I would recommend you to not being afraid of creating auxiliary variables if that makes your code more readable. You will rarely degrade your code performance for using these extra variables.
尽管我不知道你是出于简单的好奇心问这个问题,我建议你不要害怕创建辅助变量,如果这样可以让你的代码更具可读性。使用这些额外变量时,您很少会降低代码性能。
#6
What about:
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;
}
#7
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;
}
Why you'd want to do that though is beyond me... It's messy, to say the least.
为什么你想要做到这一点虽然超出了我...至少可以说它很混乱。
Perhaps:
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;
}
Or you might even consider using .IndexOf
and .Substring
instead, something like this (untested):
或者您甚至可以考虑使用.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);
}
#8
You could always use regular expressions instead of 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;
}