Bạn có thể sử dụng phương pháp split
string
lớp để chia nó trên /
Hãy thử điều này nếu bạn muốn chọn trang thư mục
string words = "http://www.example.com/directory1/directory2/default.aspx";
string[] split = words.Split(new Char[] { '/'});
string myDir=split[split.Length-2]; // Result will be directory2
Dưới đây là ví dụ từ MSDN. Cách sử dụng phương thức split
.
using System;
public class SplitTest
{
public static void Main()
{
string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";
string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });
foreach (string s in split)
{
if (s.Trim() != "")
Console.WriteLine(s);
}
}
}
// The example displays the following output to the console:
// This
// is
// a
// list
// of
// words
// with
// a
// bit
// of
// punctuation
// and
// a
// tab
// character
Bạn có thể muốn có một chút chính xác hơn: Bạn muốn subdir cuối cùng trước khi trang? tức là nếu url là 'http: // www.abc.com/foo/bar/baz/default.aspx' bạn muốn' baz'? – Filburt
Vui lòng xem câu trả lời cập nhật của tôi. – jams