StdStringTrimTest.cpp

#include <iostream>
int main()
{
std::string str(" 字符串 String ");
std::cout << str << std::endl;
std::cout << str.size() << std::endl;
str.erase(str.find_first_of(' '), str.find_first_not_of(' '));
str.erase(str.find_last_not_of(' ') + );
std::cout << str << std::endl;
std::cout << str.size() << std::endl;
return ;
}

CStyleStringTrimTest.c

#include <stdio.h>
#include <string.h>
#define STRING_LENGTH 10000
int main()
{
const char *text = " 字符串 String ";
printf("%s\n%u\n", text, strlen(text));
while (*text == ' ')
++text;
char str[STRING_LENGTH];
strcpy(str, text);
size_t i = strlen(str) - ;
while (str[i] == ' ')
str[i--] = '\0';
printf("%s\n%u\n", str, strlen(str));
return ;
}

Compilation.bat

g++ -std=c++11 StdStringTrimTest.cpp -o StdStringTrimTest
gcc -std=c11 CStyleStringTrimTest.c -o CStyleStringTrimTest

C/C++实现删除字符串的首尾空格的更多相关文章

  1. C++去掉字符串中首尾空格和所有空格

    c++去掉首尾空格是参考一篇文章的,但是忘记文章出处了,就略过吧. 去掉首尾空格的代码如下: void trim(string &s) { if( !s.empty() ) { s.erase ...

  2. JS中删除字符串中的空格

    问题描述:         在进行字符串操作时,由于字符串中存在较多的空格,因此需要考虑取消字符串中的空格 问题解决:       (1)删除字符串中的前导空格(字符串的前面的空格): 注意:这里使用 ...

  3. java 编写函数将字符串的首尾空格删除。

    String 类有个方法去除字符串首位空格: str.trim(); 查看源代码: public String trim() { int len = value.length; ; char[] va ...

  4. js 删除字符串中所有空格

    //去除头尾和中间空格,制表符 function trimSpaces(Str){               var ResultStr = "";               ...

  5. C++ 去掉字符串的首尾空格和全部空格

    #include <iostream>#include <string>using namespace std; //去掉收尾空格string& ClearHeadTa ...

  6. PHP删除字符串中的空格和换行符 将字符串中的连续多个空格转换为一个空格

    //删除空格和回车 function trimall($str){ $qian=array(" "," ","\t","\n&qu ...

  7. C++删除字符串的前后空格

    函数: string trim(string& str) { str.erase(0, str.find_first_not_of(" \t")); // 去掉头部空格 s ...

  8. [py]str list切片-去除字符串首尾空格-递归思想

    取出arr的前几项 #方法1 print([arr[0], arr[1]]) #方法2 arr2 = [] for i in range(2): arr2.append(arr[i]) print(a ...

  9. NSCharacterSet去除字符串中的空格、删除指定\任意字符集

    一.去除首尾的空格 /** 1.去除首尾的空格*/ NSString *strMsg=@" 简书作者:CoderZb "; NSString *strResult = [strMs ...

随机推荐

  1. UIAlertView+Blocks.h

    #import <Foundation/Foundation.h> typedef void (^DismissBlock)(int buttonIndex); typedef void ...

  2. BZOJ 3856: Monster【杂题】

    Description Teacher Mai has a kingdom. A monster has invaded this kingdom, and Teacher Mai wants to ...

  3. py 爬取页面http://m.sohu.com 并存储

             usage()               opts,args = getopt.getopt(sys.argv[1:],                        usage( ...

  4. P1140 相似基因 (动态规划)

    题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了4种核苷酸,简记作A,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务中,生物学家研究 ...

  5. POJ3132 Sum of Different Primes

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3473   Accepted: 2154 Description A pos ...

  6. android:logo

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  7. 在Fedora 22下安装配置RealVNC Server 5.2.3的经验总结

    RealVNC是目前功能最全.性能最好的VNC商业软件套件,很多时候为了确保性能和功能的统一,还是大量地在使用RealVNC.最近在Fedora 22工作站上安装RealVNC Server 5.2. ...

  8. 教妹学 Java:大有可为的集合

    00.故事的起源 “二哥,上一篇<泛型>的反响效果怎么样啊?”三妹对她提议的<教妹学 Java>专栏很是关心. “有人评论说,‘二哥你敲代码都敲出幻想了啊.’” “呵呵,这句话 ...

  9. T3187 队列练习3 codevs

    http://codevs.cn/problem/3187/ 题目描述 Description 比起第一题,本题加了另外一个操作,访问队头元素(编号3,保证访问队头元素时或出队时队不为空),现在给出这 ...

  10. Codeforces 540 D Bad Luck Island

    Discription The Bad Luck Island is inhabited by three kinds of species: r rocks, s scissors andp pap ...