从字符串中删除逗号 [英] strip commas from string
问题描述
大家好!我一直在寻找一种方法来从字符串中删除字符
,例如逗号。这对于使用逗号
作为分隔符非常有用。我告诉你我现在的情况。
#include< iostream>
#include< string>
int main( int argc,char * argv [])
{
使用命名空间std;
char people [14] ="我,我自己,I" ;
char nocommas [14];
int i,j;
i = j = 0;
for(i = 0; i< 14; i ++){
if(people [i]!='','')
nocommas [i ] =人民[i];
if(people [i] =='','')
nocommas [i] ='''' ;
}
cout<< 用逗号: <<人<< endl;
cout<< 没有逗号: << nocommas<< endl;
cout<< " \\\
\\\
\\\
\\\
" ;
cout<< \t现在告诉我如何将每个名字存储在一个单独的
变量中? << endl;
cout<< \t并且有人可以告诉我如何用字符串代替
of char []? << endl;
cout<< \t\t非常感谢! <<结束;
返回0;
}
所以你可以看到它会删除逗号并在其位置放置一个空格。
感谢您的帮助:-)
Hello all! I''ve been looking for a way to strip characters from strings
such as a comma. This would be great for using a comma
as a delimiter. I show you what I have right now.
#include<iostream>
#include<string>
int main(int argc, char *argv[])
{
using namespace std ;
char people[14] = "Me,Myself,I" ;
char nocommas[14] ;
int i, j ;
i=j=0 ;
for(i = 0; i < 14; i++){
if(people[i] != '','')
nocommas[i] = people[i] ;
if(people[i] == '','')
nocommas[i] = '' '' ;
}
cout << "With commas : " << people << endl ;
cout << "Without commas : " << nocommas << endl ;
cout << "\n\n\n\n" ;
cout << "\tNow tell me how I can store each name in a separate
variable ?" << endl ;
cout << "\tAnd can someone show me how to do this with string instead
of char[] ?" << endl ;
cout << "\t\t Thanks so much!" << endl ;
return 0 ;
}
so as you can see it strips commas and puts a space in its place.
Thanks for you help :-)
推荐答案
在************* @ yahoo.com 上写道:
大家好!我一直在寻找一种方法来从字符串中删除字符
,例如逗号。这对于使用逗号
作为分隔符非常有用。我告诉你我现在拥有的东西。
感谢您发布代码。这确实有帮助。
#include< iostream>
#include< string>
你没有使用std :: string。所以你_may_不需要
这个标题。
int main(int argc,char * argv [])
{
使用命名空间std;
char人[14] =我,我自己,我 ;
char nocommas [14];
int i,j;
i = j = 0;
for(i = 0; i< ; 14; i ++){
if(people [i]!='','')
nocommas [i] = people [i];
if(people [ i] =='','')
nocommas [i] ='''';
}
cout<< 用逗号: <<人<< endl;
cout<< 没有逗号: << nocommas<< endl;
cout<< " \\\
\\\
\\\
\\\
" ;
cout<< \t现在告诉我如何将每个名字存储在一个单独的
变量中? << endl;
cout<< \t并且有人可以告诉我如何用字符串来代替
char []? << endl;
cout<< \t\t非常感谢! <<结束;
返回0;
}
所以你可以看到它会删除逗号并在其位置放置一个空格。
谢谢你的帮助: - )
Hello all! I''ve been looking for a way to strip characters from strings
such as a comma. This would be great for using a comma
as a delimiter. I show you what I have right now.
Thank you for posting code. This really helps.
#include<iostream>
#include<string> You''re not using std::string. So you _may_ not need
this header.
int main(int argc, char *argv[])
{
using namespace std ;
char people[14] = "Me,Myself,I" ;
char nocommas[14] ;
int i, j ;
i=j=0 ;
for(i = 0; i < 14; i++){
if(people[i] != '','')
nocommas[i] = people[i] ;
if(people[i] == '','')
nocommas[i] = '' '' ;
}
cout << "With commas : " << people << endl ;
cout << "Without commas : " << nocommas << endl ;
cout << "\n\n\n\n" ;
cout << "\tNow tell me how I can store each name in a separate
variable ?" << endl ;
cout << "\tAnd can someone show me how to do this with string instead
of char[] ?" << endl ;
cout << "\t\t Thanks so much!" << endl ;
return 0 ;
}
so as you can see it strips commas and puts a space in its place.
Thanks for you help :-)
1.查看std :: string类,特别是构造函数:
std :: string(char *)
这样你就可以使用字符串类的find()成员了。
2.还可以探索strtok()和strchr()函数在
" C"标准库。
-
Thomas Matthew
C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt
C ++常见问题: http:// www。 parashift.com/c++-faq-lite
C常见问题: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn .c-c ++ faq:
http://www.comeaucomputing.com / learn / faq /
其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍
http://www.sgi.com/tech/stl - 标准模板库
1. See std::string class, especially the constructor:
std::string(char *)
So that you can use the find() member of the string class.
2. Also explore the strtok() and strchr() functions in the
"C" standard library.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
>大家好!我一直在寻找一种方法来从字符串
> Hello all! I''ve been looking for a way to strip characters from strings
中删除字符,例如逗号。这对于使用逗号
作为分隔符非常有用。我告诉你我现在拥有的东西。
cout<< " \\\
\\\
\\\
\\\
" ;
cout<< \t现在告诉我如何将每个名字存储在一个单独的
变量中? << endl;
cout<< \t并且有人可以告诉我如何用字符串来代替
char []? << endl;
cout<< \t\t非常感谢! << endl;
返回0;
}
such as a comma. This would be great for using a comma
as a delimiter. I show you what I have right now. cout << "\n\n\n\n" ;
cout << "\tNow tell me how I can store each name in a separate
variable ?" << endl ;
cout << "\tAnd can someone show me how to do this with string instead
of char[] ?" << endl ;
cout << "\t\t Thanks so much!" << endl ;
return 0 ;
}
存储每个名称在一个单独的变量中你会想要一个std :: vector,
另一个容器,或者一个数组。
根据逗号分割字符串,你可以放
std :: stringstream中的字符串,然后使用std :: getline:
std :: istringstream ss(" I,Myself,Me" );
std :: string name;
while(std :: getline(ss,name,'',''){
/ * push_back名称到std :: vector或另一个容器的末尾
* /
}
to store "each name in a separate variable" you''ll want a std::vector,
another container, or perhaps an array.
to split the string based upon commas, you can put the string in a
std::stringstream and then use std::getline:
std::istringstream ss("I,Myself,Me");
std::string name;
while(std::getline(ss, name, '','') {
/* push_back name onto the end of a std::vector or another container
*/
}
>
在************* @ yahoo.com 写道:
大家好!我一直在寻找一种方法从字符串中删除字符
,例如逗号。这对于使用逗号来说非常棒>作为分隔符。我现在告诉你我现在的情况。
#include< iostream>
#include< string>
int main(int argc, char * argv [])
{
使用命名空间std;
char peo ple [14] =我,我自己,我 ;
char nocommas [14];
int i,j;
i = j = 0;
for(i = 0; i< ; 14; i ++){
if(people [i]!='','')
nocommas [i] = people [i];
if(people [ i] =='','')
nocommas [i] ='''';
}
cout<< 用逗号: <<人<< endl;
cout<< 没有逗号: << nocommas<< endl;
cout<< " \\\
\\\
\\\
\\\
" ;
cout<< \t现在告诉我如何将每个名字存储在一个单独的
变量中? << endl;
cout<< \t并且有人可以告诉我如何用字符串来代替
char []? << endl;
cout<< \t\t非常感谢! <<结束;
返回0;
}
所以你可以看到它会删除逗号并在其位置放置一个空格。
谢谢你的帮助: - )
Hello all! I''ve been looking for a way to strip characters from strings
such as a comma. This would be great for using a comma
as a delimiter. I show you what I have right now.
#include<iostream>
#include<string>
int main(int argc, char *argv[])
{
using namespace std ;
char people[14] = "Me,Myself,I" ;
char nocommas[14] ;
int i, j ;
i=j=0 ;
for(i = 0; i < 14; i++){
if(people[i] != '','')
nocommas[i] = people[i] ;
if(people[i] == '','')
nocommas[i] = '' '' ;
}
cout << "With commas : " << people << endl ;
cout << "Without commas : " << nocommas << endl ;
cout << "\n\n\n\n" ;
cout << "\tNow tell me how I can store each name in a separate
variable ?" << endl ;
cout << "\tAnd can someone show me how to do this with string instead
of char[] ?" << endl ;
cout << "\t\t Thanks so much!" << endl ;
return 0 ;
}
so as you can see it strips commas and puts a space in its place.
Thanks for you help :-)
这里有一种可能性:
#include< iostream>
#include< sstream>
#include< string>
#include< list>
int main (int argc,char * argv [])
{
//逗号分隔的单词的测试字符串
char * people =" ;我,我自己,我&!;
//从'人''中创建一个名为''in''的输入流
std :: istringstream in(people);
//''words''将保存从''in''中提取的单词列表
std :: list< std :: string>单词;
//虽然没有错误''in''
而(in)
{
//'''aWord''将保留从''in'中读取的下一个字。
std :: string aWord;
/ /获取所有文本从''in''到下一个逗号或数据结尾,
//进入''aWord''
getline(in,aWord ,'','');
//如果''in''没有错误,即我们将文字读入''aWord'',
//然后将''aWord''的副本放入''words''列表
if(in)
words.push_back(aWord); < br $>
}
std :: cout<< 逗号分隔的单词从''人''读到
''单词''列表中
<< std :: endl;
//''it''是一个迭代器,用于遍历
//''words''列表的内容
std :: list< std :: string> :: iterator it;
//打印''words'中的所有条目'
for(it = words。 begin(); it!= words.end(); it ++)
std :: cout<< (* it)<< std :: endl;
返回0;
}
问候,
拉里
Here''s one possibility:
#include <iostream>
#include <sstream>
#include <string>
#include <list>
int main(int argc, char *argv[])
{
// the test string of comma-seperated words
char * people = "Me,Myself,I";
// make an input stream named ''in'' from ''people''
std::istringstream in(people);
// ''words'' will hold the list of words extracted from ''in''
std::list< std::string > words;
// while no error on ''in''
while(in)
{
// ''aWord'' will hold the next word read from ''in''
std::string aWord;
// get all text from ''in'' up to the next comma, or end of data,
// into ''aWord''
getline(in, aWord, '','');
// if no error on ''in'', i.e. we read text into ''aWord'',
// then put a copy of ''aWord'' in to the ''words'' list
if (in)
words.push_back(aWord);
}
std::cout << "Comma seperated words read from ''people'' into the
''words'' list"
<< std::endl;
// ''it'' is an iterator for iterating over the content of the
// ''words'' list
std::list< std::string >::iterator it;
// print all of the entries in ''words''
for (it = words.begin(); it != words.end(); it++)
std::cout << (*it) << std::endl;
return 0;
}
Regards,
Larry
这篇关于从字符串中删除逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!