How to remove the last part of a URL after the last slash (/) in C++

2 Answers

0 votes
#include <iostream>

using std::cout;
  
int main() {
    std::string url = "https://www.website.com/abc/xyz";
 
    url = url.substr(0, url.rfind("/"));
      
    cout << url;
}
  
  
  
/*
run:
  
https://www.website.com/abc
  
*/

 



answered Feb 20, 2020 by avibootz
0 votes
#include <iostream>
 
using std::cout;
   
int main() {
    std::string url = "https://www.website.com/abc/xyz";
  
    int pos = url.rfind("/");
    
    if (pos != std::string::npos) {
        url = url.substr(0, url.rfind("/"));
        cout << url;
    }
    else {
        cout << "not found";   
    }
}
   
   
   
/*
run:
   
https://www.website.com/abc
   
*/

 



answered Feb 20, 2020 by avibootz

Related questions

2 answers 257 views
1 answer 165 views
1 answer 260 views
1 answer 208 views
1 answer 189 views
...