获取URL的最后一个路径

时间:2022-10-20 10:04:39

I'm using the following code which is working just fine for most of the services but some time in the URL last I got User;v=2;mp and I need to get just User,how should I handle it? I need some general solution since I guess in some other URL I can get different spacial charters

我正在使用以下代码,这对大多数服务都运行得很好,但是在URL中有一段时间我得到了User; v = 2; mp我需要得到User,我该如何处理它?我需要一些通用的解决方案,因为我猜在其他一些网址中我可以得到不同的空间包机

if the URL

如果是URL

https://ldmrrt.ct/odata/GBSRM/User;v=2;mp
string serviceName = _uri.Segments.LastOrDefault();

if the URL is https://ldmrrt.ct/odata/GBSRM/User its working fine...

如果URL是https://ldmrrt.ct/odata/GBSRM/User,它的工作正常......

2 个解决方案

#1


6  

Just replace:

string serviceName = _uri.Segments.LastOrDefault();

With:

string serviceName = _uri.Segments.LastOrDefault().Split(new[]{';'}).First();

If you need something more flexible, where you can specify what characters to include or skip, you could do something like this (slightly messy, you should probably extract parts of this as separate variables, etc):

如果你需要更灵活的东西,你可以指定要包含或跳过的字符,你可以做这样的事情(稍微凌乱,你应该把它的一部分作为单独的变量提取等):

// Note the _ and - characters in this example:
Uri _uri = new Uri("https://ldmrrt.ct/odata/GBSRM/User_ex1-ex2;v=2;mp");

// This will return a string: "User_ex1-ex2"
string serviceName =
        new string(_uri.Segments
                          .LastOrDefault()
                          .TakeWhile(c => Char.IsLetterOrDigit(c)
                                          || (new char[]{'_', '-'}).Contains(c))
                          .ToArray());

Update, in response to what I understand to be a question below :) :

更新,以回应我理解为下面的问题:):

You could just use a String.Replace() for that, or you could use filtering by doing something like this:

您可以使用String.Replace(),或者您可以通过执行以下操作来使用过滤:

// Will return: "Userexex2v=2mp"
string serviceName = 
             new string(_uri.Segments
                            .LastOrDefault()       
                            .Where(c => 
                                    !Char.IsPunctuation(c) // Ignore punctuation
                                    // ..and ignore any "_" or "-":
                                    && !(new char[]{'_', '-'}).Contains(c)) 
                             .ToArray());

If you use this in production, mind you, be sure to clean it up a little, e.g. by splitting into several variables, and defining your char[]-array prior to usage.

如果你在生产中使用它,请注意,一定要清理一下,例如通过拆分成几个变量,并在使用之前定义你的char [] - 数组。

#2


2  

In your case you can try to split a "User;v=2;mp" string.

在您的情况下,您可以尝试拆分“User; v = 2; mp”字符串。

string [] split = words.Split(new Char [] { ';' });

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.

返回一个字符串数组,该数组包含此实例中由指定的Unicode字符数组的元素分隔的子字符串。

split.First();

or:

split[0];

#1


6  

Just replace:

string serviceName = _uri.Segments.LastOrDefault();

With:

string serviceName = _uri.Segments.LastOrDefault().Split(new[]{';'}).First();

If you need something more flexible, where you can specify what characters to include or skip, you could do something like this (slightly messy, you should probably extract parts of this as separate variables, etc):

如果你需要更灵活的东西,你可以指定要包含或跳过的字符,你可以做这样的事情(稍微凌乱,你应该把它的一部分作为单独的变量提取等):

// Note the _ and - characters in this example:
Uri _uri = new Uri("https://ldmrrt.ct/odata/GBSRM/User_ex1-ex2;v=2;mp");

// This will return a string: "User_ex1-ex2"
string serviceName =
        new string(_uri.Segments
                          .LastOrDefault()
                          .TakeWhile(c => Char.IsLetterOrDigit(c)
                                          || (new char[]{'_', '-'}).Contains(c))
                          .ToArray());

Update, in response to what I understand to be a question below :) :

更新,以回应我理解为下面的问题:):

You could just use a String.Replace() for that, or you could use filtering by doing something like this:

您可以使用String.Replace(),或者您可以通过执行以下操作来使用过滤:

// Will return: "Userexex2v=2mp"
string serviceName = 
             new string(_uri.Segments
                            .LastOrDefault()       
                            .Where(c => 
                                    !Char.IsPunctuation(c) // Ignore punctuation
                                    // ..and ignore any "_" or "-":
                                    && !(new char[]{'_', '-'}).Contains(c)) 
                             .ToArray());

If you use this in production, mind you, be sure to clean it up a little, e.g. by splitting into several variables, and defining your char[]-array prior to usage.

如果你在生产中使用它,请注意,一定要清理一下,例如通过拆分成几个变量,并在使用之前定义你的char [] - 数组。

#2


2  

In your case you can try to split a "User;v=2;mp" string.

在您的情况下,您可以尝试拆分“User; v = 2; mp”字符串。

string [] split = words.Split(new Char [] { ';' });

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.

返回一个字符串数组,该数组包含此实例中由指定的Unicode字符数组的元素分隔的子字符串。

split.First();

or:

split[0];