如何从字符串 c++ 中删除子字符串
我有一个 string s="home/dir/folder/name"
我想在 s1="home/dir/folder"
中拆分 s
> 和 s2="name";
我做到了:
char *token = strtok( const_cast<char*>(s.c_str() ), "/" );
std::string name;
std::vector<int> values;
while ( token != NULL )
{
name=token;
token = strtok( NULL, "/" );
}
现在s1=name
。 s2
怎么样?
I have a string s="home/dir/folder/name"
I want to split s
in s1="home/dir/folder"
and s2="name";
I did:
char *token = strtok( const_cast<char*>(s.c_str() ), "/" );
std::string name;
std::vector<int> values;
while ( token != NULL )
{
name=token;
token = strtok( NULL, "/" );
}
now s1=name
. What about s2
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我建议不要使用
strtok
。看看 Boost Tokenizer (这里有一些示例)。或者,要简单地查找最后一个
'/'
的位置,您可以使用std::string::rfind
:I'd recommend against using
strtok
. Take a look at Boost Tokenizer instead (here are some examples).Alternatively, to simply find the position of the last
'/'
, you could usestd::string::rfind
:如果您的目标只是获取
string
中最后一个\
或/
的位置,您可以使用 string::find_last_of 正是这样做的。从那里,您可以使用 string::substr 或
std::string
使用迭代器来获取您想要的子部分。只需确保原始字符串至少包含
\
或/
,或者您正确处理了这种情况。这是一个函数,它可以执行您需要的操作并返回一个包含路径的两部分的
pair
。如果指定的路径不包含任何\
或/
字符,则整个路径将作为该对的第二个成员返回,第一个成员为空。如果路径以/
或\
结尾,则第二个成员为空。当然,您也可以修改此函数以抛出错误,而不是在路径不具有预期格式时优雅退出。
If your goal is only to get the position of the last
\
or/
in yourstring
, you might use string::find_last_of which does exactly that.From there, you can use string::substr or the constructor for
std::string
that takes iterators to get the sub-part you want.Just make sure the original string contains at least a
\
or/
, or that you handle the case properly.Here is a function that does what you need and returns a
pair
containing the two parts of the path. If the specified path does not contain any\
or/
characters, the whole path is returned as a second member of the pair and the first member is empty. If the path ends with a/
or\
, the second member is empty.Of course you might as well modify this function to throw an error instead of gracefully exiting when the path does not have the expected format.
几点:第一,你使用
strtok
是未定义的行为;在对于 g++ 来说,它甚至可能导致一些非常奇怪的行为。你
无法修改字符串后面的
std::string
的内容并期望能够逃脱惩罚。 (
const_cast
的必要性应该已经向您透露了信息。)
其次,如果您要操作文件名,我强烈建议
推荐
boost::filesystem
。它知道所有关于路径之类的事情分隔符等,以及路径的最后一个组成部分
通常是特殊的(因为它可能是文件名,而不是目录)。
第三,如果这只是其中之一,或者由于其他原因你不能或
不想使用 Boost:
将为您提供一个指向最后一个“/”之后的第一个字符的迭代器,或者
到字符串中的第一个字符(如果没有)。在那之后,
使用 string 的两个迭代器构造函数来获取很简单
两个组件:
如果您以后必须支持 Windows,只需将
find
替换为:Several points: first, your use of
strtok
is undefined behavior; inthe case of g++, it could even lead to some very strange behavior. You
cannot modify the contents of an
std::string
behind the strings backand expect to get away with it. (The necessity of a
const_cast
shouldhave tipped you off.)
Secondly, if you're going to be manipulating filenames, I'd strongly
recommend
boost::filesystem
. It knows all about things like pathseparators and the like, and the fact that the last component of a path
is generally special (since it may be a filename, and not a directory).
Thirdly, if this is just a one-of, or for some other reason you can't or
don't want to use Boost:
will give you an iterator to the first character after the last '/', or
to the first character in the string if there isn't one. After that,
it's a simple to use the two iterator constructors of string to get the
two components:
And if you later have to support Windows, just replace the
find
with:查看Boost 字符串分割。
例子:
Check out boost string split.
Example:
您不能在
std::string
上使用strtok
。strtok
将修改字符串。它破坏了c_str()
契约。执行
const_cast<>
是错误的一个重要标志。You can't use
strtok
onstd::string
.strtok
would modify the string. It break thec_str()
contract.Doing
const_cast<>
is a big sign for error.只需使用字符串方法:
两点建议:
Just use the string methods:
Two bits of advice: