【问题标题】:Remove the unwanted spaces but between the values删除不需要的空格,但在值之间
【发布时间】:2020-02-09 07:05:25
【问题描述】:
Input -> Miguel de Cervantes,     Don Quixote,     Spain,    9.99,    1612,   The story follows a Hidalgo, Alonso Quixano, who proves that chivalry will in fact never die.

问题

  • 如何删除值之间的空格而不是它们内部的空格?
  • 示例输出:Miguel de Cervantes,Don Quixote,Spain,.99,1612,The story follows a Hidalgo,Alonso Quixano,who proves that chivalry will in fact never die.

谢谢

【问题讨论】:

  • std::regex 怎么样? std::string result = std::regex_replace(input, std::regex re{" *, *"}, ",");
  • @0x5453 对我不起作用
  • 是否有任何答案对您有用?如果是,则通过接受答案将此线程标记为已解决。

标签: c++ string c++11


【解决方案1】:
#include <iostream>
#include <iterator>
#include <regex>
#include <string>

int main()
{
   std::string text = "Miguel de Cervantes,     Don Quixote,     Spain,    9.99,    1612,   The story follows a Hidalgo, Alonso Quixano, who proves that chivalry will in fact never die.";
   std::regex vowel_re(",\\s+");
   std::cout << '\n' << std::regex_replace(text, vowel_re, ",") << '\n';
}

Output

Miguel de Cervantes,Don Quixote,Spain,9.99,1612,The story follows a Hidalgo,Alonso Quixano,who proves that chivalry will in fact never die.

我使用的正则表达式是,\\s+,即一个逗号后跟许多空格。

Note: Thank you! Because of you i wrote C++ after 5 years!

【讨论】:

    猜你喜欢
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 2023-03-22
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    相关资源
    最近更新 更多