C++ 从字符串中修剪/删除制表符 ( "\t" )
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/556277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trim / remove a tab ( "\t" ) from a string
提问by AndyUK
Can anyone suggest a way of stripping tab characters ( "\t"s ) from a string? CString or std::string.
任何人都可以建议一种从字符串中剥离制表符( "\t"s )的方法吗?CString 或 std::string。
So that "1E10 " for example becomes "1E10".
因此,例如“1E10”变为“1E10”。
Thanks in anticipation.
感谢期待。
回答by Luc Touraille
If you want to remove all occurences in the string, then you can use the erase/removeidiom:
如果要删除字符串中的所有出现,则可以使用擦除/删除习语:
#include <algorithm>
s.erase(std::remove(s.begin(), s.end(), '\t'), s.end());
If you want to remove only the tab at the beginning and end of the string, you could use the boost string algorithms:
如果只想删除字符串开头和结尾的选项卡,可以使用boost 字符串算法:
#include <boost/algorithm/string.hpp>
boost::trim(s); // removes all leading and trailing white spaces
boost::trim_if(s, boost::is_any_of("\t")); // removes only tabs
If using Boost is too much overhead, you can roll your own trim function using find_first_not_of
and find_last_not_of
string methods.
如果使用 Boost 的开销太大,您可以使用find_first_not_of
和find_last_not_of
字符串方法滚动您自己的修剪功能。
std::string::size_type begin = s.find_first_not_of("\t");
std::string::size_type end = s.find_last_not_of("\t");
std::string trimmed = s.substr(begin, end-begin + 1);
回答by j_random_hacker
hackingwords' answergets you halfway there. But std::remove()
from <algorithm>
doesn't actually make the string any shorter -- it just returns an iterator saying "the new sequence would end here." You need to call my_string().erase()
to do that:
hackingwords 的回答让你成功了一半。但是std::remove()
from<algorithm>
实际上并没有使字符串更短——它只是返回一个迭代器,说“新序列将在这里结束”。你需要打电话my_string().erase()
来做到这一点:
#include <string>
#include <algorithm> // For std::remove()
my_str.erase(std::remove(my_str.begin(), my_str.end(), '\t'), my_str.end());
回答by Konrad Rudolph
The remove
algorithm shifts all characters not to be deleted to the beginning, overwriting deleted characters but it doesn't modify the container's length (since it works on iterators and doesn't know the underlying container). To achieve this, call erase
:
该remove
算法将所有不被删除的字符移到开头,覆盖已删除的字符,但它不会修改容器的长度(因为它适用于迭代器并且不知道底层容器)。为此,请致电erase
:
str.erase(remove(str.begin(), str.end(), '\t'), str.end());
回答by Stefan
Since others already answered how to do this with std::string, here's what you can use for CString:
由于其他人已经回答了如何使用 std::string 执行此操作,因此您可以将其用于 CString:
myString.TrimRight( '\t' ); // trims tabs from end of string
myString.Trim( '\t' ); // trims tabs from beginning and end of string
if you want to get rid of all tabs, even those inside the string, use
如果您想删除所有选项卡,甚至是字符串内的选项卡,请使用
myString.Replace( _T("\t"), _T("") );
回答by graham.reeds
HackingWords is nearly there: Use erase in combination with remove.
HackingWords 即将到来:将擦除与移除结合使用。
std::string my_string = "this\tis\ta\ttabbed\tstring";
my_string.erase( std::remove( my_string.begin(), my_string.end(), '\t'), my_string.end());
回答by sharptooth
Scan the string and remove all the found occurences.
扫描字符串并删除所有找到的出现。
回答by RvdK
CString replace?
CString 替换?
replace('\t', '')
替换('\t', '')
回答by drby
回答by user5680820
I wonder why no one trims a string this way:
我想知道为什么没有人以这种方式修剪字符串:
void trim (string& s) {
string t = "";
int i = 0;
while(s[i] == ' ') i++;
while(s[i] == '\t') i++;
for(i; i < s.length(); i++)
t += s[i];
s = t;
}