C++ 删除空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8394930/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:30:22  来源:igfitidea点击:

C++ remove whitespace

c++stringstlstd

提问by tr0yspradling

I have this code to remove whitespace in a std::string and it removes all characters after the space. So if I have "abc def" it only returns "abc". How do I get it to go from "abc def ghi" to "abcdefghi"?

我有这个代码来删除 std::string 中的空格,它删除空格后的所有字符。所以如果我有“abc def”,它只返回“abc”。我如何让它从“abc def ghi”变成“abcdefghi”?

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

int main(int argc, char* argv[]) {
    std::string input, output;
    std::getline(std::cin, input);

    for(int i = 0; i < input.length(); i++) {
        if(input[i] == ' ') {
            continue;
        } else {
            output += input[i];
        }
    }
    std::cout << output;
    std::cin.ignore();
}

回答by Ry-

The issue is that cin >> inputonly reads until the first space. Use getline()instead. (Thanks, @BenjaminLindley!)

问题是cin >> input只能读取到第一个空格。使用getline()来代替。(谢谢,@BenjaminLindley!)

回答by Vijay

Well the actual problem you had was mentioned by others regarding the cin >>But you can use the below code for removing the white spaces from the string:

好吧,其他人提到了您遇到的实际问题,cin >>但您可以使用以下代码从字符串中删除空格:

str.erase(remove(str.begin(),str.end(),' '),str.end());

回答by Jerry Coffin

Since the >>operator skips whitespace anyway, you can do something like:

由于>>运算符无论如何都会跳过空格,因此您可以执行以下操作:

while (std::cin>>input)
    std::cout << input;

This, however, will copy the entire file (with whitespace removed) rather than just one line.

但是,这将复制整个文件(删除空格)而不仅仅是一行。

回答by Aleksandar

My function for removing a character is called "conv":

我删除字符的函数称为“conv”:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string conv(string first, char chr)
{
    string ret,s="x";
    for (int i=0;i<first.length();i++)
    {
        if (first[i]!=chr)
        s=s+first[i]; 
    }
    first=s;
    first.erase(0,1);
    ret=first;
    return ret;
    }
int main()
{
    string two,casper="testestestest";
    const char x='t';
    cout<<conv(casper,x);
    system("PAUSE");
    return 0;
}

You need to change the const char xto ' '(whitespace, blanco) for the job to be done. Hope this helps.

您需要将const char xto ' '(whitespace, blanco)更改为要完成的工作。希望这可以帮助。

回答by Vyas Ramankulangara

ifstream ifs(filename);
string str, output;
vector<string> map;
while (getline(ifs, str, ' ')) {
    map.push_back(str);
}
for(int i=0; i < map.size();i++){
    string dataString = map[i];
    for(int j=0; j < dataString.length(); j++){
        if(isspace(dataString[j])){
            continue;
        }
        else{
            output +=dataString[j];
        }
    }
}