zoukankan      html  css  js  c++  java
  • c++基础学习之string

     
    //学习使用string类 2013-10-18 lingc
    #include <iostream>
    #include <string>//include this head file <string>
    using namespace std;
    
    int main()
    {
        string myString1;
        myString1 = "hello world,i am lingc!";// can no use the ' ' 
        string myString2(myString1,5,12);// begin at index 5, and count 12 elements
        string myString3(4,'z');
        cout<<"how to use the class of string,by lingc at 2013-10-18"<<endl;
        cout<<"myString1 is "<< myString1 <<endl;
        cout<<"myString2 is "<< myString2 <<endl;
        cout<<"myString3 is ""<< myString3 <<"" and its length is "
                << myString3.length() <<endl;
    
        //combine the string
        string comString = myString1 + " " + "i am so sleepy.";
        
        cout << comString <<endl;
        //the follow is wrong 不能用+来连接字符串字面量
        //string comString2 = " " + "so sleepy " + myString1;
        //corect is to the right
        string comString2 = " " + ("so sleepy " + myString1);
        cout<< comString2 << endl;
    
        return 0;
    }

    可以使用string[] 来访问字符串中的字符:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string mystring = "hello world.";
        cout<< mystring <<endl;
        for (size_t i=0; i<mystring.length(); i++)
        {
            mystring[i] = toupper(mystring[i]);
        }
        cout<< mystring <<endl;
        return 0;
    }

     

  • 相关阅读:
    js数组
    关于编程,程序员的一些语录
    css心得
    js函数
    一些电脑基础知识
    gnome3安装
    C学习小记
    ubuntu重装系统后
    elinks文字浏览器
    快捷方式
  • 原文地址:https://www.cnblogs.com/lingc/p/3376389.html
Copyright © 2011-2022 走看看