zoukankan      html  css  js  c++  java
  • 字符串的相关处理问题

    • itoa函数  

    原型:char *itoa( int value, char *string,int radix) ,其中的字符指针的参数既可以是数组名字(自动转化为指向数组的指针),也可以是动态申请的内存的指针。代码如下:

    使用声明的字符数组:

    #include "stdafx.h"
    #include <iostream>

    using namespace std;


    int _tmain(int argc, _TCHAR* argv[])
    {
     int i = 12;
     char p[10] = {0}; //数组声明的时候全部初始化为0即'\0'

     itoa(i, p, 10);
     

     cout<<p<<endl;

     return 0;
    }

    动态申请字符数组:

    #include "stdafx.h"
    #include <iostream>

    using namespace std;


    int _tmain(int argc, _TCHAR* argv[])
    {
     int i = 12;
     char *p = new char[10]; 

     itoa(i, p, 10);
     

     cout<<p<<endl;

     return 0;
    }

    • 空字符的问题

    #include "stdafx.h"
    #include <iostream>

    using namespace std;


    int _tmain(int argc, _TCHAR* argv[])
    {
     int i = 12;
     char p [10];

       itoa(i, p, 10);
     
        p[9] = '\0';
     cout<<p<<p[9]<<"dd"; //怎么跟空格一个效果,空格和空字符什么关系

     cout<<p[9]<<endl;

     return 0;
    }

    空字符和空格字符的比较:

    #include "stdafx.h"
    #include <iostream>

    using namespace std;


    int _tmain(int argc, _TCHAR* argv[])
    {
     int i = 12;
     char p [10];

       itoa(i, p, 10);
     
        p[9] = '\0';
     cout<<p<<p[9]<<"dd";

     if (' ' == 0) //空格字符
     {
      cout<<"aaaa"<<endl; //此处无输出
     }
     else if ('\0' == 0)
     {
      cout<<"dddd"<<endl; //此处有输出
     }

     return 0;
    }

    • 格式化输出到字符数组:

    sprintf

    sprint_s

     待完善

  • 相关阅读:
    Vue 路由组件传参的 8 种方式
    JS中通过url动态获取图片大小的方法小结(两种方法)
    基于 Vue.js 实现的精致移动端组件库
    .net core 通过代码创建数据库表
    .net core 框架调用顺序
    POCO的理解
    winform datagridview 同步滚动
    UseIIS
    winform 多个datagridview 之间同步滚动
    winform BackgroundWorker 的用法
  • 原文地址:https://www.cnblogs.com/laorenjia/p/2145584.html
Copyright © 2011-2022 走看看