获取分隔符为/的给定字符串的最后一个目录的文本
Getting the text of the last directory of a given string with the delimiters of /
给定一个URL(它是一个字符串),例如:
www.testsite.com/pictures/banners/whatever/
我希望能够获得URL中最后一个目录的字符(在这种情况下,它是"whatever",我还想删除斜杠)。最有效的方法是什么?
感谢您的帮助
#include <iostream>
#include <string>
std::string getlastcomponent(std::string s) {
if (s.size() > 0 && s[s.size()-1] == '/')
s.resize(s.size() - 1);
size_t i = s.find_last_of('/');
return (i != s.npos) ? s.substr(i+1) : s;
}
int main() {
std::string s1 = "www.testsite.com/pictures/banners/whatever/";
std::string s2 = "www.testsite.com/pictures/banners/whatever";
std::string s3 = "whatever/";
std::string s4 = "whatever";
std::cout << getlastcomponent(s1) << 'n';
std::cout << getlastcomponent(s2) << 'n';
std::cout << getlastcomponent(s3) << 'n';
std::cout << getlastcomponent(s4) << 'n';
return 0;
}
获取长度并从最后一个字母开始推送每个字母(例如伪代码:
x = string.length()
while(X != 0)
{
CharVector.push(string.at(x));
x--;
if(string.at(x) == "") break;
}
然后你得到了复仇而不是什么。然后用这个功能交换:
string ReverseString( const string& word )
{
std::string l_bla;
bla.reserve(word.size());
for ( string::size_type x = word.length ( ); x > 0; x-- )
{
l_bla += word.at ( x -1 );
}
return l_bla;
}
所以你得到了任何
相关文章:
- lower_bound()返回最后一个元素
- C++试图读取一个文件并输出到另一个文本文件
- 获取向量C++中第一个值和最后一个值的和
- 获取用C/C++打印的最后一个字符串
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 为什么我的最后一个 ELSE 条件无法正确执行
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 我的C++程序只写入文本文件最后一个条目
- 如何确定文本中的最后一个字符串值
- C++ 输出在读取文本文件时跳过最后一个单词
- 如何将文本(诗歌)拆分为行(字符串/字符[])并查找每个行的最后一个单词
- 为什么当我在Visual c ++ studio 2010中运行应用程序时光标会转到最后一个文本框
- 获取分隔符为/的给定字符串的最后一个目录的文本
- 如何删除文本文件C++中的最后一个字符
- 读取文本文件并在C/C++中每行只得到最后一个整数的结果
- 最后一个单词在文本文件C++中重复
- 屏幕没有清除最后一个文本为什么?
- 如何在读取文本文件时跳到每行最后一个数字之后的下一行
- 文本的最后一个辅音