How to Get the Last Path in a Url

Last segment of URL with JavaScript

You can also use the lastIndexOf() function to locate the last occurrence of the / character in your URL, then the substring() function to return the substring starting from that location:

console.log(this.href.substring(this.href.lastIndexOf('/') + 1));

That way, you'll avoid creating an array containing all your URL segments, as split() does.

How to get the last path in a URL?

Try:

$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];

Assuming $tokens has at least 2 elements.

How to obtain the last path segment of a URL

Use path.Base to get the last element of a path: path.Base(myUrl.Path)

Run it on the playground.

Get last path from URL

Just do TrimEnd at the end

public static String GetUserNameInstagramUrl(String url)
{
Uri uri = new Uri(url);
return uri.Segments.Last().TrimEnd('/');
}

Is possible to get last path of RegExp in URL

/([a-z\d]+)(\/*|)$/i

And see "$1"

Get URL Path without last segment

Using pop and URL api

this assumes the URL is not likely to change

I use document.URL since that is what is recommended

const url = new URL("https://www.example.com/first/second/last"); // new URL(document.URL)
let path = url.pathname.split("/");
path.pop(); // remove the last
url.pathname = path.join("/")
console.log(url)

Get the last path of URL

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:

// 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.

remove last directory in URL

Explanation: Explode by "/", remove the last element with pop, join again with "/".

function RemoveLastDirectoryPartOf(the_url)
{
var the_arr = the_url.split('/');
the_arr.pop();
return( the_arr.join('/') );
}

see fiddle http://jsfiddle.net/GWr7U/

How to obtain the last path segment of a URI

is that what you are looking for:

URI uri = new URI("http://example.com/foo/bar/42?param=true");
String path = uri.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
int id = Integer.parseInt(idStr);

alternatively

URI uri = new URI("http://example.com/foo/bar/42?param=true");
String[] segments = uri.getPath().split("/");
String idStr = segments[segments.length-1];
int id = Integer.parseInt(idStr);


Related Topics



Leave a reply



Submit