English 中文(简体)
使用c#进行扼杀?
原标题:find string using c#?

我正试图在下文中找到一个扼杀。

http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx?ID=1779

by using http://example.com/TIGS/SIM/Lists string. How can I get Team Discussion word from it?

有时会进行扼杀。

   http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx?ID=1779
     I need `Team Discussion`

http://example.com/TIGS/ALIF/Lists/Artifical Lift Discussion Forum 2/DispForm.aspx?ID=8

    I need  `Artifical Lift Discussion Forum 2`
最佳回答

如果你想看到一个经常表达的例子:

        string input = "http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx?ID=1779";
        string given = "http://example.com/TIGS/SIM/Lists";
        System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(given + @"/(.+)/");
        System.Text.RegularExpressions.Match match = regex.Match(input);
        Console.WriteLine(match.Groups[1]); // Team Discussion
问题回答

If you re always following that pattern, I recommend @Justin s answer. However, if you want a more robust method, you can always couple the System.Uri and Path.GetDirectoryName methods, then perform a String.Split. Like this example:

String url = @"http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx?ID=1779";
System.Uri uri = new System.Uri(url);
String dir = Path.GetDirectoryName(uri.AbsolutePath);
String[] parts = dir.Split(new[]{ Path.DirectorySeparatorChar });
Console.WriteLine(parts[parts.Length - 1]);

The only major problem, however, is you re going to wind up with a path that s been "encoded" (i.e. your space is now going to be represented by a %20)

这一解决办法将使你获得最新版的URL,而不论有多少名录存放在URL。

string[] arr = s.Split( / );
string lastPart = arr[arr.Length - 2];

你们可以把这一解决办法合并成一条线,但是,这要求分两条,一对数值,二对长度。

这里采取简单的做法,假设你们的《欧洲刑法》在你们想要之前总是有同样数量的冲突:

var value = url.Split(new[]{ / }, StringSplitOptions.RemoveEmptyEntries)[5];

另一种解决办法提供了以下优势:

  • Does not require the use of regular expressions.
  • Does not require a certain count of slashes be present (indexing based of a specific number). I consider this a key benefit because it makes the code less likely to fail if some part of the URL changes. Ultimately it is best to base your parsing logic off which part of the text s structure you consider least likely to change.

This method, however, DOES rely on the following assumptions, which I consider to be the least likely to change:

  • URL must have "/Lists/" right before target text.
  • URL must have "/" right after target text.

基本上,我只用我预期会环绕我感兴趣的地区的文本,两度分开。

String urlToSearch = "http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx";
String result = "";

// First, get everthing after "/Lists/"
string[] temp1 = urlToSearch.Split(new String[] { "/Lists/" }, StringSplitOptions.RemoveEmptyEntries);                
if (temp1.Length > 1)
{
    // Next, get everything before the first "/"
    string[] temp2 = temp1[1].Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
    result = temp2[0];
}

然后,你的答复将存放在结果变量中。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签