Use lastIndexOf() to find the position of the last slash and get the part before the slash with substring().
str.substring(0, str.lastIndexOf("https://stackoverflow.com/"));
Use lastIndexOf() to find the position of the last slash and get the part before the slash with substring().
str.substring(0, str.lastIndexOf("https://stackoverflow.com/"));