我初学,莫笑。呵呵!

1.去除字符串前后空格:

string trim(string &str)
{
    
if (str.empty()) 
    {
        
return str;
    }
    str.erase(
0,str.find_first_not_of(" "));
    str.erase(str.find_last_not_of(
" "+ 1);
    
return str;
}

当然去除前端空格,或者去除末尾空格都已经明了。

2.文本文件读取,文件内容键值对 ,以‘=’分隔:

    string strtmp;
    ifstream fs(
"Test.txt");
    map
<string,string>  map_temp;
    
string::size_type index = string::npos;

    
while(getline(fs,strtmp,'\n'))
    {
        index 
= strtmp.find("=");
        map_temp[trim(strtmp.substr(
0,index-1))] = trim(strtmp.substr(++index));
    }
    map
<string,string> ::iterator it = map_temp.begin();

    
for(; it != map_temp.end(); it++)
    {
        cout 
<< it->first << "=" << it->second << endl;
    }

注意:
string s = "aa=" ; 其中 ‘=’索引为2,s.substr(0,1) 返回的将是"aa".




posted on 2008-07-24 01:36 -274°C 阅读(370) 评论(0)  编辑  收藏 所属分类: C++

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 909090
  • 排名 - 40

最新评论