我有以下字符串:
string source = "Test/Company/Business/Department/Logs.tvs/v1";
/
字符是字符串中各个元素之间的分隔符。我需要获取字符串的最后两个元素。为此,我有以下代码。这工作正常。有没有更快/更简单的代码?
代码
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();
}