[转]C++ string的trim, split方法
很多其他语言的libary都会有去除string类的首尾空格的库函数,但是标准C++的库却不提供这个功能。但是C++string也提供很强大的功能,实现trim这种功能也不难。下面是几种方法:
1.使用string的find_first_not_of,和find_last_not_of方法
- /*
- Filename : StringTrim1.cpp
- Compiler : Visual C++ 8.0
- Description : Demo how to trim string by find_first_not_of & find_last_not_of
- Release : 11/17/2006
- */
- #include <iostream>
- #include <string>
- std::string& trim(std::string &);
- int main()
- {
- std::string s = " Hello World!! ";
- std::cout << s << " size:" << s.size() << std::endl;
- std::cout << trim(s) << " size:" << trim(s).size() << std::endl;
- return 0;
- }
- std::string& trim(std::string &s)
- {
- if (s.empty())
- {
- return s;
- }
- s.erase(0,s.find_first_not_of(" "));
- s.erase(s.find_last_not_of(" ") + 1);
- return s;
- }
2.使用boost库中的trim,boost库对提供很多C++标准库没有但是又非常常用和好用的库函数,例如正则表达式,线程库等等。
- /*
- Filename : boostStringTrim.cpp
- Compiler : Visual C++ 8.0 / ISO C++ (boost)
- Description : Demo how to boost to trim string
- Release : 02/22/2007 1.0
- */
- #include <iostream>
- #include <string>
- #include <boost/algorithm/string.hpp>
- using namespace std;
- using namespace boost;
- int main() {
- string s = " hello boost!! ";
- trim(s);
- cout << s << endl;
- }
3.使用template(我用GCC编译不通过,用VS2005却可以)
- /*
- Filename : stringTrim1.cpp
- Compiler : Visual C++ 8.0
- Description : Demo how to trim string by other method.
- Release : 11/18/2006
- */
- #include <string>
- #include <iostream>
- #include <cwctype>
- template <class T>
- std::basic_string<T>& trim(std::basic_string<T>&);
- int main( )
- {
- std::string s = " Hello World!! ";
- std::cout << s << " size:" << s.size() << std::endl;
- std::cout << trim(s) << " size:" << trim(s).size() << std::endl;
- return 0;
- }
- template <class T>
- std::basic_string<T>& trim(std::basic_string<T>& s)
- {
- if (s.empty()) {
- return s;
- }
- std::basic_string<T>::iterator c;
- // Erase whitespace before the string
- for (c = s.begin(); c != s.end() && iswspace(*c++);); s.erase(s.begin(), --c);
- // Erase whitespace after the string
- for (c = s.end(); c != s.begin() && iswspace(*--c);); s.erase(++c, s.end());
- return s;
- }
split方法
- //注意:当字符串为空时,也会返回一个空字符串
- void split(std::string& s, std::string& delim,std::vector< std::string >* ret)
- {
- size_t last = 0;
- size_t index=s.find_first_of(delim,last);
- while (index!=std::string::npos)
- {
- ret->push_back(s.substr(last,index-last));
- last=index+1;
- index=s.find_first_of(delim,last);
- }
[转]C++ string的trim, split方法的更多相关文章
- String类的split方法以及StringTokenizer
split方法可以根据指定的表达式regex将一个字符串分割成一个子字符串数组. 它的参数有两种形式,也即:split(String regex)和split(String regex, int li ...
- java中String对象的split方法
在java.lang包中有String.split()方法,返回是一个String[]数组,今天碰到一个自己没注意的问题: 1.特殊分隔符 String str1 = "123|456|78 ...
- java基础---->String中的split方法的原理
这里面主要介绍一下关于String类中的split方法的使用以及原理. split函数的说明 split函数java docs的说明: When there is a positive-width m ...
- 【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!
split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator],[limit])参数:stringObj 必选项.要被分解的 ...
- JAVA中string类的split方法
split([separator,[limit]])第一个参数为分隔符,可以是一个正则表达式,第二个参数为返回结果数组的长度
- String.split()方法你可能不知道的一面
一.问题 java中String的split()是我们经常使用的方法,用来按照特定字符分割字符串,那么我们看以下一段代码: public void splitTest() { String str = ...
- String的split方法,你真的懂吗
String的split方法相信大家都不陌生,或多或少都用过它将字符串转成一个数组,但是就是这样一个简单的方法,里面也有一个不得不注意.不深不浅的小坑. 本地测试代码如下图所示: 图1 大家会发现sp ...
- 自己实现String.prototype.trim方法
今天呢 知乎看到一道题 说是网易面试题,要求自己写一个trim()方法, 实现 var str = " a sd "; 去掉字符串两端的空格. 直接上码 var str ...
- C# String.split()用法小结。String.Split 方法 (String[], StringSplitOptions)
split()首先是一个分隔符,它会把字符串按照split(' 字符')里的字符把字符串分割成数组,然后存给一个数组对象. 输出数组对象经常使用foreach或者for循环. 第一种方法 string ...
随机推荐
- HTML <img> 标签的 height 和 width 属性
定义和用法 <img> 标签的 height 和 width 属性设置图像的尺寸. 提示:为图像指定 height 和 width 属性是一个好习惯.如果设置了这些属性,就可以在页面加载时 ...
- android 自定义弹出框AlertDialog ,很炫的哦
于是就小小的模仿了下自己写了这个这样的效果,主要代码如下:dlg = new AlertDialog.Builder(context).create();dlg.show();dlg.getWin ...
- GPS基础
public class MainActivity extends Activity { private LocationManager manager; private List<String ...
- Linux常用命令_(文件搜索)
文件查找主要包含以下几个命令 which.whereis.grep.find.wc
- 直接操作游戏对象C#游戏开发
直接操作游戏对象C#游戏开发 2.2.3 直接操作游戏对象 在Inspector视图里通过设置属性而改变游戏场景中游戏对象的状态,太过抽象,毕竟数字并不够直观.其实,改变游戏对象的状态,完全有最最直 ...
- Ajax本地跨域问题
问题:打开本地html文件时,,报错如下 Cross origin requests are only supported for protocol schemes: http, data,chrom ...
- 看病要排队(stl)
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- DP(优化) UVALive 6073 Math Magic
/************************************************ * Author :Running_Time * Created Time :2015/10/28 ...
- 水题 ZOJ 3869 Ace of Aces
题目传送门 水题,找出出现次数最多的数字,若多个输出Nobody //#include <bits/stdc++.h> //using namespace std; #include &l ...
- BZOJ4347 : [POI2016]Nim z utrudnieniem
将石子从小到大排序,然后DP. 设$f[i][j][k]$表示考虑了前$i$堆的石子,当前扔掉的堆数模$d$为$j$,没有扔掉的石子的异或和为$k$的方案数. 因为石子排过序,所以转移的复杂度为$O( ...