简体   繁体   English

在字符串c#的第三次出现后如何获取子字符串

[英]How get a substring after the 3rd occurrence of a string c#

How can I find use the substring method so I can get everything after the 3rd occurence of "/" for example I have string that contains: http://TEST.COM/page/subpage 如何找到使用substring方法的方法,以便在出现“ /”的第3次之后获得所有内容,例如,我的字符串包含:http: //TEST.COM/page/subpage

How can I extract page/subpage this from the above string (in c#)? 如何从上面的字符串(在C#中)提取页面/子页面?

If you are working with URL's you can use Uri class: 如果您使用的是URL,则可以使用Uri类:

var url = new Uri("http://TEST.COM/");
var path = url.MakeRelativeUri(new Uri("http://TEST.COM/page/subpage"));

You can use split() : 您可以使用split():

    // The directory 
    string dir = "http://TEST.COM/page/subpage";
    // Split on directory separator
    string[] parts = dir.Split('/');

And you will have an array. 您将拥有一个数组。 You can do with it what you want. 您可以根据需要使用它。 And Split() string as you wish. 和Split()字符串,如你所愿。

There could be various ways: 可能有多种方式:

As @Selman mentioned using Uri class: 正如@Selman提到的使用Uri类:

var url = new Uri("http://TEST.COM/");
var path = url.MakeRelativeUri(new Uri("http://TEST.COM/page/subpage"));

using IndexOf 使用IndexOf

var offset = myString.IndexOf('/');
offset = myString.IndexOf('/', offset+1);
var result = myString.IndexOf('/', offset+1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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