C++寻找 String.Replace()

发布于 2024-09-07 00:47:31 字数 416 浏览 7 评论 0原文

我在 C++ 中有一个 char 数组,看起来像 {'a','b','c',0,0,0,0}

现在我将其写入流,我希望它看起来像“abc”,有四个空值插入空格 我主要使用 std::stiring 并且我也有 boost。 我怎样才能在 C++ 中做到这一点

基本上我想我正在寻找类似的东西

char hellishCString[7] = {'a','b','c',0,0,0,0}; // comes from some wired struct actually...
std::string newString(hellishCString, sizeof(hellishCString));

newString.Replace(0,' '); // not real C++

ar << newString;

i have a a char array in C++ which looke like {'a','b','c',0,0,0,0}

now im wrting it to a stream and i want it to appear like "abc " with four spaces insted of the null's
i'm mostly using std::stiring and i also have boost.
how can i do it in C++

basicly i think im looking for something like

char hellishCString[7] = {'a','b','c',0,0,0,0}; // comes from some wired struct actually...
std::string newString(hellishCString, sizeof(hellishCString));

newString.Replace(0,' '); // not real C++

ar << newString;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-09-14 00:47:31

使用 std::replace :

#include <string>
#include <algorithm>
#include <iostream>

int main(void) {
  char hellishCString[7] = {'a','b','c',0,0,0,0}; // comes from some wired struct actually...
  std::string newString(hellishCString, sizeof hellishCString);
  std::replace(newString.begin(), newString.end(), '\0', ' ');
  std::cout << '+' << newString << '+' << std::endl;
}

Use std::replace:

#include <string>
#include <algorithm>
#include <iostream>

int main(void) {
  char hellishCString[7] = {'a','b','c',0,0,0,0}; // comes from some wired struct actually...
  std::string newString(hellishCString, sizeof hellishCString);
  std::replace(newString.begin(), newString.end(), '\0', ' ');
  std::cout << '+' << newString << '+' << std::endl;
}
段念尘 2024-09-14 00:47:31

如果用向量替换数组,还有另一种解决方案

#include <vector> 
#include <string>
#include <algorithm>
#include <iostream>


char replaceZero(char n)
{
    return (n == 0) ? ' ' : n;
}

int main(int argc, char** argv)
{
    char hellish[] = {'a','b','c',0,0,0,0};
    std::vector<char> hellishCString(hellish, hellish + sizeof(hellish));    
    std::transform(hellishCString.begin(), hellishCString.end(), hellishCString.begin(), replaceZero);
    std::string result(hellishCString.begin(), hellishCString.end());
    std::cout << result;
    return 0;
}

One more solution if you replace an array by the vector

#include <vector> 
#include <string>
#include <algorithm>
#include <iostream>


char replaceZero(char n)
{
    return (n == 0) ? ' ' : n;
}

int main(int argc, char** argv)
{
    char hellish[] = {'a','b','c',0,0,0,0};
    std::vector<char> hellishCString(hellish, hellish + sizeof(hellish));    
    std::transform(hellishCString.begin(), hellishCString.end(), hellishCString.begin(), replaceZero);
    std::string result(hellishCString.begin(), hellishCString.end());
    std::cout << result;
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文