【发布时间】:2021-01-12 17:08:30
【问题描述】:
我有以下模板 std::string
std::string myString = R"svg(
<svg height={size} width={size}>
<rect {...rectProps} x={x0h} y={y0h} />
// <rect {...rectProps} x={x1h} y={y0h} />
<rect {...rectProps} x={x0h} y={y1h} />
// <rect {...rectProps} x={x1h} y={y1h} />
</svg>
)svg";
我想删除以//开头的每一行
所以我想要的结果会是这样的
<svg height={size} width={size}>
<rect {...rectProps} x={x0h} y={y0h} />
<rect {...rectProps} x={x0h} y={y1h} />
</svg>
编辑:所以我现在的想法是迭代每一行,修剪它,然后检测它是否以“//”开头
到目前为止,我有这个伪
std::istringstream stream(myString);
std::string line;
std::string myFinalString;
while (std::getline(stream, line)) {
// TODO: trim line first
bool isComment = false; // Find here if the line starts with //
if (!isComment) {
myFinalString += line + "\n";
}
}
【问题讨论】:
-
这里有一个简单的方法来弄清楚如何做到这一点,它永远不会失败。只需拿出一张白纸。用简单的英语用简短的句子写下来,这是一个循序渐进的过程。完成后,call your rubber duck for an appointment。我们不会在 Stackoverflow 上为其他人编写代码。我们总是向您的橡皮鸭提出此类问题。在您的橡皮鸭批准您提出的行动计划后,只需将您写下的内容直接翻译成 C++。任务完成!
-
您可以使用boost::algorith::trim 删除空格并使用boost::algorith::starts_with 检查
"//" -
" 但没有按我的预期工作" 不是有用的评论。更好的是告诉你得到什么结果以及你期望什么。还包括可能的错误消息。
-
@ThomasSablik 对于 std::string 是这样的吗?
-
“每一行”是包含在文件中还是要直接从源代码中删除行?