Saturday, June 29, 2013

C++(beginner) string type and its important functions

String is a set of characters.It can contain any character.We declare a string variable like this "string s;" here s is our string.For declaring a string we must first include the string class (look in an intermediate section to learn  classes) it by writing it in the top of our code. ( #include <string>)
First important thing is to know how to call a character from a string.There are a few ways to do that.First is all known [] operator.
Imagine we have string s="ABCDEF";
s[1] will be 'B'.(note that the numerating of characters is starting from 0 not 1 in other words s[0]=A, s[1]=B, s[2]=C;}
other way of calling a character is the function called "at".We write it like this s.at(int b); As we can see this function requires a parameter.
b is the parameter and it is the index of the character we want to call.this function returns character so we need to have a separate character to use this function.(char c; c=s.at(1); here c will be equal to 'B';).This way is much safer than the previous one.
Another way is to go with a loop if we need all of the characters and use the iterators to call them.This is the safest and the fastest way.(Learn about the iterators in C++ intermediate section)
Adding character to the string.
we have string s.We can add a character to the end of the string buy using a very useful and fast function called push_back.It looks like this s.push_back(char c).c must be a character.Sometimes people get errors which are very common. note that when you use a character you write it with this symbol '. With strings " symbol.char c='a' but string s="aaaa".It is also possible to add a character to the end of string by just writing s=s+c;(c is a character).It is also possible to add a character from other places of the string.We have a useful function called insert. We can use insert function to add a character or even a group of characters in the middle of a string.
This is it's construction s.insert(int index,string str), here we are adding str starting from index in our string .For example we have
s="AAAAAAA"; and we have str="BB"; If we write s.insert(1,str); now s will be ABBAAAAAA;
We have a function for checking is our string empty or not.It looks like this s.empty() (note that this function has no parameters and is a boolean function which returns true or false).
function erase.
function erase looks like this s.erase(index1,number)those are two integers which is needed to say from what point we want to erase and how many symbols we want to erase.Example string s="ABCCCDA"; s.erase(1,3), now our string will be "ACDA";
function find.
this is one of the most important functions .This function is searching for a string in a string.Example we have string s="ABCCDAA";
and string str="BCC"; now if we want to find str in s we write s.find(str); if program finds str in s it will return the index the starting point of the string found in s.
String type and its functionsAnd for checking how long our string is we write function called size. int b=s.size(); it returns an integer a number of characters in the string.

No comments:

Post a Comment