从 C++ 中的字符串中删除空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16329358/
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
Remove spaces from a string in C++
提问by mikey
I am currently learning C++. I am trying to code a method to remove white spaces form a string and return the string with no spaces This is my code:
我目前正在学习 C++。我正在尝试编写一种方法来从字符串中删除空格并返回没有空格的字符串这是我的代码:
string removeSpaces(string input)
{
int length = input.length();
for (int i = 0; i < length; i++) {
if(input[i] == ' ')
input.erase(i, 1);
}
return input
}
But this has a bug as it won't remove double or triple white spaces. I found this on the net
但这有一个错误,因为它不会删除双倍或三倍空格。我在网上找到了这个
s.erase(remove(s.begin(),s.end(),' '),s.end());
but apparently this is returning an iterator(if I understand well)
Is there any way to convert the iteratorback to my string input?
Most important is this the right approach?
但显然这是返回一个iterator(如果我理解得很好)有什么方法可以将其转换iterator回我的字符串input?最重要的是这是正确的方法吗?
回答by Vaughn Cato
std::string::erasereturns an iterator, but you don't have to use it. Your original string is modified.
std::string::erase返回一个迭代器,但您不必使用它。您的原始字符串已修改。
string removeSpaces(string input)
{
input.erase(std::remove(input.begin(),input.end(),' '),input.end());
return input;
}
回答by Shafik Yaghmour
std::remove_ifalong with erasewould be much easier (see it live):
的std ::的remove_if沿erase会容易得多(现场观看):
input.erase(remove_if(input.begin(), input.end(), isspace),input.end());
using std::isspacehad the advantage it will capture all types of white space.
使用std::isspace的优势在于它可以捕获所有类型的空白。
回答by Adrian Panasiuk
Let's assume your input has a double space, for example "c++[ ][ ]is[ ]fun" ([ ]represents a single space). The first space has index 3 (numeration starts from 0) and the second space, is of course index 4.
假设您的输入有两个空格,例如“c++[ ][ ]is[ ]fun”([ ]代表一个空格)。第一个空格的索引为 3(从 0 开始编号),第二个空格当然是索引 4。
In your forloop, when you hit i == 3you erase the first space. The next iteration of the loop takes i == 4as the index. But is the second space at index 4 now ? No! Removing the first space changed the string into "c++[ ]is[ ]fun": the space to remove is at index 3, again!
在您的for循环中,当您点击时,您i == 3会擦除第一个空格。循环的下一次迭代将i == 4作为索引。但是现在是索引 4 处的第二个空格吗?不!删除第一个空格将字符串更改为“c++[]is[]fun”:要删除的空格再次位于索引 3 处!
The solution can be to remove spaces right-to-left:
解决方案可以是从右到左删除空格:
for (int i = length-1; i >= 0; --i) {
if(input[i] == ' ')
input.erase(i, 1);
}
This solution has the benefit of being simple, but as Tony D points out, it's not efficient.
此解决方案的优点是简单,但正如 Tony D 指出的那样,它效率不高。
回答by Bill
this should also work -- std::replace( input.begin(), input.end(), ' ', '');You need to include <algorithm>
这也应该有效—— std::replace( input.begin(), input.end(), ' ', '');你需要包括<algorithm>
回答by Karan Dhingra
this code should work
这段代码应该可以工作
string removeSpaces(string input)
{
int length = input.length();
for (int i = 0; i < length; i++) {
if(input[i] == ' ')
{
input.erase(i, 1);
length--;
i--;
}
}
return input
}
Reason: if it gets space in the string it will reduce the length of the string, so you have to change the variable: "length" accordingly.
原因:如果它在字符串中获得空间,它将减少字符串的长度,因此您必须相应地更改变量:“length”。
回答by dolev ben
I tried to write something to. This function take a string and copy to another temporary string all the content without extra spaces.
我试着写点什么。此函数取一个字符串并将所有内容复制到另一个临时字符串中,没有多余的空格。
std::string trim(std::string &str){
int i = 0;
int j = 0;
int size = str.length();
std::string newStr;
bool spaceFlag = false;
for(int i = 0;i < size; i++){
if(str[i] == ' ' && (i+1) < size && str[i+1] == ' '){
i++;
spaceFlag = true;
continue;
}
if(str[i] == ' '){
newStr += " ";
continue;
}
if(str[i] == '\t' && i != 0){
str[i] = ' ';
newStr += " ";
}
else{
newStr += str[i];
if(spaceFlag){
newStr += " ";
spaceFlag = false;
}
}
}
str = newStr;
return str;
}
}
回答by Prince Kumar Sharma
#include<iostream>
#include<string.h>
using namespace std;
void trimSpace(char s[])
{
int i=0, count=0, j=0;
while(s[i])
{
if(s[i]!=' ')
s[count++]=s[i++];
else {
s[count++]=' ';
while(s[i]==' ')
i++;
}
}
s[count]='##代码##';
cout<<endl<<" Trimmed String : ";
puts(s);
}
int main()
{
char string[1000];
cout<<" Enter String : ";
gets(string);
trimSpace(string);
return 0;
}

