去除字符串首尾空格
2011-12-15 19:57
260 查看
方法1:
function trim(str){
return str.replace(/(^\s*)|(\s*$)/g, "");
}
方法2:
int main()
{
std::string str = " hello world! ";
std::string trimstring = " ";
str.erase(str.find_last_not_of(trimstring)+1);
std::cout << "after right trim: " << str << “end” << std::endl; //输出"end"便于测试
str.erase(0,str.find_first_not_of(trimstring));
std::cout << "after left trim: " << str << std::endl;
return 0;
}
//写成函数的形式
string trim(string& s, CONST string& drop)
{
//trim right
s.erase(s.find_last_not_of(drop)+1);
//trim left
return s.erase(0,s.find_first_not_of(drop));
}
function trim(str){
return str.replace(/(^\s*)|(\s*$)/g, "");
}
方法2:
int main()
{
std::string str = " hello world! ";
std::string trimstring = " ";
str.erase(str.find_last_not_of(trimstring)+1);
std::cout << "after right trim: " << str << “end” << std::endl; //输出"end"便于测试
str.erase(0,str.find_first_not_of(trimstring));
std::cout << "after left trim: " << str << std::endl;
return 0;
}
//写成函数的形式
string trim(string& s, CONST string& drop)
{
//trim right
s.erase(s.find_last_not_of(drop)+1);
//trim left
return s.erase(0,s.find_first_not_of(drop));
}
相关文章推荐
- 【美团校招笔试题】去除字符串首尾空格,中间多个空格只保留一个
- c\c++语言去除字符串首尾空格
- ios 去除字符串首尾空格、换行
- 学习笔记之Python 切片:利用切片操作,实现一个trim()函数,去除字符串首尾的空格
- [py]str list切片-去除字符串首尾空格-递归思想
- 用JavaScript去除字符串的首尾空格。
- 【廖雪峰 python教程 课后题 切片】利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:
- php去除字符串首尾空格(包括全角)(转)
- Java去除字符串多余空格以及首尾空格
- 利用切片操作,实现一个trim()函数,去除字符串首尾的空格
- js---在前端开发中,如何通过正则表达式去除字符串string首尾的空格,JavaScript正则去除首尾空格
- iOS开发之NSString字符串去掉首尾空格、去除空格
- <Mooc C++ 教程作业>去除字符串首尾多余的空格
- AS3 去除字符串首尾空格
- ios 去除字符串首尾空格、换行
- 去除字符串首尾的所有空格,使用切片法!!!
- C++去除字符串首尾和串中多余空格
- 如何c++去除string字符串首尾的\n,\r,\t,空格?
- 去除字符串首尾的空格
- C语言去除字符串首尾空格,trim()函数实现