Getting a substring between two delimiters in an arbitrary position

I have the following line:

string source = "Test/Company/Business/Department/Logs.tvs/v1";

A character /is a separator between the various elements in a string. I need to get the last two elements of a row. I have a code for this purpose. It works great. Is there a faster / simpler code for this?

CODE

    static void Main()
    {
        string component = String.Empty;
        string version = String.Empty;
        string source = "Test/Company/Business/Department/Logs.tvs/v1";
        if (!String.IsNullOrEmpty(source))
        {
            String[] partsOfSource = source.Split('/');
            if (partsOfSource != null)
            {
                if (partsOfSource.Length > 2)
                {
                    component = partsOfSource[partsOfSource.Length - 2];
                }

                if (partsOfSource.Length > 1)
                {
                    version = partsOfSource[partsOfSource.Length - 1];
                }
            }
        }

        Console.WriteLine(component);
        Console.WriteLine(version);
        Console.Read();
    }
+5
source share
7 answers

Why is there no regular expression? This is pretty easy:

.*/(?<component>.*)/(?<version>.*)$

You can even tag your groups so that for your compliance there is everything you need to do:

component = myMatch.Groups["component"];
version = myMatch.Groups["version"];
+4
source

The following should be faster, since it scans as many lines as needed to find two /, and this does not interfere with splitting the entire line:

string component = "";
string version = "";
string source = "Test/Company/Business/Department/Logs.tvs/v1";
int last = source.LastIndexOf('/');
if (last != -1)
{
    int penultimate = source.LastIndexOf('/', last - 1);
    version = source.Substring(last + 1);
    component = source.Substring(penultimate + 1, last - penultimate - 1);
}

, : ! , .

( , , , ... , source null, lazy me.)

+3

. :

  • String.Split() null, .
  • source /, ? ( ​​ )
  • , ( )? , , .
+2

, . LINQ, , , .

, Microsoft LINQ. , . , LINQ, RegEx.

EDIT: +1 Matt RegEx... , .

+2

- , , . System.Diagnostics.StopWatch, , .

string source = "Test/Company/Business/Department/Logs.tvs/v1";

int index1 = source.LastIndexOf('/');
string last = source.Substring(index1 + 1);

string substring = source.Substring(0, index1);
int index2 = substring.LastIndexOf('/');
string secondLast = substring.Substring(index2 + 1);
+1

        string source = "Test/Company/Business/Department/Logs.tvs/v1";

        var components = source.Split('/').Reverse().Take(2);

        String last = string.Empty;

        var enumerable = components as string[] ?? components.ToArray();
        if (enumerable.Count() == 2)
            last = enumerable.FirstOrDefault();
        var secondLast = enumerable.LastOrDefault();

Hope this helps

+1
source

You can get the last two words using the following process:

string source = "Test/Company/Business/Department/Logs.tvs/v1";

 String[] partsOfSource = source.Split('/');
if(partsOfSourch.length>2)
 for(int i=partsOfSourch.length-2;i<=partsOfSource.length-1;i++)
console.writeline(partsOfSource[i]);
-1
source

All Articles