The class apstring is a class that handles strings. Strings are basically anything with quotes around them. This is an extremely versatile class that truly expands a programmer's ability to handle strings. In order for the class apstring to work, you must first possess some files. If you want them, just ask me and I'll send them to you. At the top of your program you will add the .h (header) file by putting #include "apstring.h" at the top of the program. This will be explained in greater detail later. If you want to use apstring, you must first declare an instance of it. Apstring is a user defined data type, so it is declared like all other data types.
There. You just declared a variable named thestring of the data type apstring. Now to set thestring to something. To do this, you simply have to set it equal (remember, keep the variable on the left side of the equal sign) to a string of text, contained in between quotation marks. For example:
thestring = "My first string!" ;
Piece of cake! Now whenever you place the variable thestring in a cout statement, it will print out "My fist string!" without the quotes. You can also perform normal number operations with it, but now you must use text in quotes instead of numbers. In addition, you can add two strings together. Here are some examples:
apstring string1 = "My " ; apstring string2 = "First " ; apstring string3 = "String! " ; apstring string4; string4 = string1 + string2 + string3;
In this problem, string4 will equal "My First String!".
string1 += string2;
Here, string1 = string1 + string2. This makes string1 = "My First ".
string2 = string1 + " String!"
And finally, string2 = "My First String!".
Apstring doesn't actually save the quotes in the string, but I was just placing them in there to show you what was saved in the string and what wasn't. I'll bet that right now you are asking yourself "Just how do you allow the USER to input a string?" Quite simple, really. Just do this:
apstring mystring; getline(cin, mystring);
Remember, you must declare the variable before you can set it equal to something!
That command is used in place of the cin >> command. Simply use the exact same syntax with your variable in place of mystring. Alright, now on to the meat of this class. These functions are what makes the apstring class so versatile. They allow you to have nearly complete control over strings. Here we go!
apstring string = "My Second String"; apstring string1; int x; x = string.length();
The length() function finds the number of characters in the given string.
x = string.find("i");
The find("text") function looks for the text, in this case i, in the given string. It returns the index of the location of the letter, or -1 if the letter isn't in the string. The index is a method of counting which starts from 0 instead of 1. So the 4th letter in the string would be the 3rd index. The 1st letter in the string would be the 0th index. Don't you dare ask me why they have this, because I can't figure it out either. You just gotta live with it...*sigh*
And as with almost everything in programming, it doesn't have to be straight text - it can be a variable that contains text. Another way to use indexes with strings is like this:
string1 = string;
This finds the 5th index character in the string. It, of course, doesn't have to be 5. It can be any number, or any variable with an integer in it. Remember, start counting from 0! Here, string1 would equal "c", again without the quotes. Here is something else you can do with 's:
string = string;
Simply amazing. This changes the 5th character (using indexing to count) to the 8th character (indexing) in the string. Be careful when doing this though. If you use a number that is bigger than the actual string, then the program will shut down.
A very versatile ability of apstring is to make substrings, which are "cutouts" of an original string. They are made like this:
string1 = string.substr(3,13);
OK, here goes. Substrings are basically exerpts from a string. The 3 shown above is the index of the starting point of the exerpt, and the 13 is how many characters (not indexing) the exerpt counts starting from the beginning of the exerpt. So given string = "My String is Cool", string.substr(3,13) would return "tring is Cool". A bit confusing at first, but you'll get the hang of it with use.
Lastly, you can compare strings with the normal operators. (==), (>=), etc...I can only assume that it handles (>) and (<) like any dictionary. It compares the first letters, then second, then third, etc until the letters don't match. (==) and (!=) are easy, so no explanation needed for them.