Menu

Fashion Trendy
  • Drop Down

    • Abstract
    • Model
    • Techo
    • Options
  • Photography Pictures Product

    Drop Menu

    • Crystal
    • Digital
    • Graphs
    • Settings
  • Menu

    Stack Now | C, C++, Linux, Design Patterns etc

    • Home
    • Digital Art
      • Pics
        • SEO 1
        • SEO 2
      • CSS
        • CSS 1
        • CSS 2
        • CSS 3
        • CSS 4
        • CSS 5
      • Jquery
        • Jquery 1
        • Jquery 2
    • Fashion
      • Product 1
        • Sub Item
        • Sub Item
      • Product 2
        • Sub Item
        • Sub Item
    • Photography
    • Design
    Go
    Home » C++ » Programming Tips » 8 C++ String Functions You Must Know!

    8 C++ String Functions You Must Know!

    C++ String Functions you must know.jpg
    In C++, String is one of the most used class to represent a sequence of characters. String class is an instantiation of the basic_string class template that uses char as the character type.

    Check out the below C++ string functionalities you must know:

    1. Converting C++ String into C String:
    With c_str function, you can covert the C++ string into a C string i.e null-terminated sequence of characters. Check out the example about c_str below:
    int main()
    {
      std::string str("Hello");
      char buf[10];
      strcpy(buf, str.c_str());
      std::cout<<buf<<std::endl;
      return 0;
    }


    2. Knowing Length of String:
    There are two functions to know the length of a string - size() and length()
    Check out the example about size and length functions below:
    int main()
    {
      std::string str("My Length")
      std::cout<<str.size()<<" "<<str.length()<<std::endl;
      /* will output 9 in both cases */
      return 0;
    }


    3. Clearing a String:
    clear() function will erase all the content of the string and makes it empty.
    int main()
    {
      std::string str("Testing Clear Function");
      str.clear();
      return 0;
    }


    4. Check whether String is Empty or Not:
    For this case, you can use empty() function of string class.
    void func(string str)
    {
      if (str.empty())
         return;
    }


    5. Know String value at Specific Position:
    If you want to get the value at specific position of a string, you can use operator[] or at() function
    void func()
    {
      std::string str("0123456789");
      std::cout<<str[5]; //will print 5

      std::cout<<str.at(0); //will print 0
    }


    6.Appending a string:
    To append a string to other string you can use operator+= or append() function.
    void func()
    {
      std::string str("stacknow");
      std::string str2("com");
      str += ".";
      str += str2;
      /* str will be stacknow.com */
    }


    7.Erasing Few characters in string:
    To erase a few characters in the string you can use erase() function.
    void func()
    {
      std::string str ("I LOVE YOU");
      str.erase (1,5);  /* will erase from 1st character and 5 characters length */  
      std::cout << str << '\n';
      /* output will be "I YOU" */
    }


    8.Searching a sequence in string:
    You can use find() function for searching a sequence inside a string.
    void func()
    {
      std::string str1 ("I am reading in stacknow.com");
      std::size_t found = str.find("reading");
      std::cout<< found << std::endl;
      /* found will be 5, the first occurence value */
    }


    For more C++ string functions, you can visit HERE.
    yash
    Add Comment
    C++ Programming Tips
    Wednesday, March 19, 2014

    facebook

    twitter

    google+

    fb share

    About yash

    Related Posts
    < Previous Post Next Post >

    Menu
    • Home
    • C
    • C++
    • Linux
    • Design Patterns
    • Programming Tips

    Popular Posts

    • How to Write Better Programs in C/C++?
      I have been coding for the past 7 years in a MNC and had experienced a lot of programming rules. Here I am sharing some of the tips with w...
    • Singleton Design Pattern - My View
      I had used singleton design pattern in one of my projects. And in this post I will express my views about it. Singleton design pattern i...
    • 8 C++ String Functions You Must Know!
      In C++, String is one of the most used class to represent a sequence of characters. String class is an instantiation of the basic_string c...
    • How to Print Time in Particular Format in C/C++?
      You can print time in any format using strftime function in C/C++. The function declaration is as below: size_t strftime (char* buf, i...
    • 2-Minute Guide for Patching and Generating a Patch File
      Many times in the project, you need to generate a patch file or patch the file to your project. This 2 minute guide will help you how to g...

    Social Share

    Weekly Posts

    • How to Write Better Programs in C/C++?
      I have been coding for the past 7 years in a MNC and had experienced a lot of programming rules. Here I am sharing some of the tips with w...
    • Singleton Design Pattern - My View
      I had used singleton design pattern in one of my projects. And in this post I will express my views about it. Singleton design pattern i...
    • 8 C++ String Functions You Must Know!
      In C++, String is one of the most used class to represent a sequence of characters. String class is an instantiation of the basic_string c...
    • How to Print Time in Particular Format in C/C++?
      You can print time in any format using strftime function in C/C++. The function declaration is as below: size_t strftime (char* buf, i...
    • 2-Minute Guide for Patching and Generating a Patch File
      Many times in the project, you need to generate a patch file or patch the file to your project. This 2 minute guide will help you how to g...

    Like us On Facebook

    Labels

    • C
    • C++
    • Design Patterns
    • linux
    • Programming Tips

    Copyright Stack Now | C, C++, Linux, Design Patterns etc 2014 . Template Created by