Getting arguments from URL How do I get arguments from the URL? in other words, what is the equivalent of the arg() function of Drupal 7 and previous versions?
As reported in the arg() is deprecated and will be removed change record, the Drupal 8 (procedural) equivalent of arg() is the following one.

$current_path = \Drupal::service('path.current')-&gt;getPath();
$path_args = explode('/', $current_path);


As the change record makes notice, not all the times that is the correct code to use. Depending from the context, you should use code similar to the following one.


Check if a path is for an administrative page  

if (\Drupal::service('router.admin_context')-&gt;isAdminRoute()) {
  // Your code.
}

Add CSS or JavaScript conditionally, basing on the page being visited  

function mymodule_page_attachments(array &amp;$attachments) {
  if (\Drupal::routeMatch()-&gt;getRouteName() == 'my.route') {
    $attachments ['#attached']['library'][] = 'my_module/my_library'; // you'll need to define in mymodule.libraries.yml
  }
}

Load the currently viewed node

$node = \Drupal::routeMatch()-&gt;getParameter('node');



Notice that in the first and last case, in Drupal 7 you should not use arg(), but respectively path_is_admin() and menu_get_object(). The typical beginner-Drupal-developer error is using arg() also in those cases.