std string使用 (二)

2014-11-24 11:48:40 · 作者: · 浏览: 1
n) 将字符串Str内始于位置Stridx且长度为strlen的部分,当作字符串S的初值string s(cstr) 以C-String cstr作为S的初值
string s(num,c) 生成一个字符串,包含Num个C字符
string s(beg,end) 以区间[beg,end]内的字符作为s初值
s.~string() 销毁所有字符,释放内存
注意:
std::string s('x');//error
std::string s(1,'x'); //ok,create a string that has one charactor 'x'

9、substr(string.substr的方法)

(1)参数
start:Number -- 一个整数,指示 my_str 中用于创建子字符串的第一个字符的位置。如果 start 为一个负数,则起始位置从字符串的结尾开始确定,其中 -1 表示最后一个字符。
length:Number -- 要创建的子字符串中的字符数。如果没有指定 length,则子字符串包括从字符串开头到字符串结尾的所有字符。
(2)返回
String -- 指定字符串的子字符串。
(3)示例
下面的示例创建一个新字符串 my_str,并使用 substr() 返回该字符串中的第二个单词;首先,使用正的 start 参数,然后使用负的 start 参数:
var my_str:String = new String("Hello world");
var mySubstring:String = new String();
mySubstring = my_str.substr(6,5);
trace(mySubstring); // 输出:world

mySubstring = my_str.substr(-5,5);
trace(mySubstring); // 输出:world

toupper, tolower
地球人都知道 C++ 的 string 没有 toupper ,好在这不是个大问题,因为我们有 STL 算法:

string s("heLLo");
transform(s.begin(), s.end(), s.begin(), toupper);
cout << s << endl;
transform(s.begin(), s.end(), s.begin(), tolower);
cout << s << endl;

当然,我知道很多人希望的是 s.to_upper() ,但是对于一个这么通用的 basic_string 来说,的确没办法把这些专有的方法放进来。如果你用 boost stringalgo ,那当然不在话下,你也就不需要读这篇文章了。

------------------------------------------------------------------------
trim
我们还知道 string 没有 trim ,不过自力更生也不困难,比 toupper 来的还要简单:

string s(" hello ");
s.erase(0, s.find_first_not_of(" \n"));
cout << s << endl;
s.erase(s.find_last_not_of('' '') + 1);
cout << s << endl;

注意由于 find_first_not_of 和 find_last_not_of 都可以接受字符串,这个时候它们寻找该字符串中所有字符的 absence ,所以你可以一次 trim 掉多种字符。

-----------------------------------------------------------------------
erase
string 本身的 erase 还是不错的,但是只能 erase 连续字符,如果要拿掉一个字符串里面所有的某个字符呢?用 STL 的 erase + remove_if 就可以了,注意光 remove_if 是不行的。

string s(" hello, world. say bye ");
s.erase(remove_if(s.begin(),s.end(),
bind2nd(equal_to(), '' '')),
s.end());

上面的这段会拿掉所有的空格,于是得到 hello,world.saybye。

-----------------------------------------------------------------------
replace
string 本身提供了 replace ,不过并不是面向字符串的,譬如我们最常用的把一个 substr 换成另一个 substr 的操作,就要做一点小组合:

string s("hello, world");
string sub("ello, ");
s.replace(s.find(sub), sub.size(), "appy ");
cout << s << endl;

输出为 happy world。注意原来的那个 substr 和替换的 substr 并不一定要一样长。

-----------------------------------------------------------------------
startwith, endwith
这两个可真常用,不过如果你仔细看看 string 的接口,就会发现其实没必要专门提供这两个方法,已经有的接口可以干得很好:

string s("hello, world");
string head("hello");
string tail("ld");
bool startwith = s.compare(0, head.size(), head) == 0;
cout << boolalpha << startwith << endl;
bool endwith = s.compare(s.size() - tail.size(), tail.size(), tail) == 0;
cout << boolalpha << endwith << endl;

当然了,没有 s.startwith("hello") 这样方便。

------------------------------------------------------------------------
toint, todouble, tobool...
这也是老生常谈了,无论是 C 的方法还是 C++ 的方法都可以,各有特色:

string s("123");
int i = atoi(s.c_str());
cout << i << endl;

int ii;
stringstream(s) >> ii;
cout << ii << endl;

string sd("12.3");
double d = atof(sd.c_str());
cout << d << endl;

double dd;
stringstream(sd) >> dd;
cout << dd << endl;

string sb("true");
bool b;
stringstream(sb) >> boolalpha >> b;
cout << boolalpha << b << endl;

C 的方法很简洁,而且赋值与转换在一句里面完成,而 C++ 的方法很通用。

------------------------------------------------------------------------
split
这可是件麻烦事,我们最希望的是这样一个接口: s.split(vect, '','') 。用 STL 算法来做有一定难度,我们可以从简单的开始,如