简体   繁体   English

如何使用Split()重构此C#代码?

[英]How can I refactor this C# code using Split()?

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;
}
var    index = line.LastIndexOf (marker) ;
return index < 0 ? string.Empty : line.Substring (index + marker.Length) ;

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;
}

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. 更快,资源更少。

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];

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. 使用这些额外变量几乎不会降低代码性能。

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

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;
}

You could always use regular expressions instead of split: 您可以始终使用正则表达式而不是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM