C++
String Proces
Login
We sometimes need to do a change on strings. Therefore I am goint to mention String Process
Defining Strings
We know "How can we defining a string variable" but I will explain one more way. We can defin strings like that :
std::string str1("texts");
Adding a string on other string
We typ + for adding strings. An example:
std::string name("kadir");
std::string surname("çamuryapan");
std::string str1 = name + surname;
Comparing Strings
We need know "How can we compare 2 string" before If else lesson. We use "=="
std::string name("kadir");
std::string surname("kadir");
std::bool = (name == surname);
Length or String Size
We use length() or size() commands to find length of string.
std::string name("kadir");
int uzunluk = name.length();
Reach to Char of String
We can reach a char of string.We use that synstax:
nameofvariable[indexofchar]
An example :
std::string name("kadir");
std::cout<<name[2];
Find Command
We can check Is a string in another string or not ? If yes, where ? with this command. We use like that:
nameofvariable.find("text",n);
N mean is C++ start searching which first index. C++ return index of char when firstly see this string on other string.
Substring
We can select a part of string. We use substr() command like that :
nameofvariable.substr(indexofbegin,n);
N mean is How many index after begin index for cutting ?
Erasing a Part of String
If we want to delete a part of string, we use erase() comannd to do. Using rules is like substring:
nameofvariable.erase(indexofbegin,n);
N mean is how many do you want delete after begin index.
Insert Command
We can add a string to other string. But we will see how can we add a string after any index.
nameofvariable.insert(indexofbeginadding,"text");
Comments
Post a Comment