A Study of VCL Strings |
|
axsoft
版主 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
A Study of VCL Strings
To declare a string, use the AnsiString word followed by a valid C name. Here is an example:
Since the AnsiString is a class with its own constructor, you can also declare its variable with empty parenthese, which would be calling the class’ construtor. Here is an example:
There are two main ways you can initialize an AnsiString variable. After declaring it, you can assign the desired value to the variable using the assignment operator. Here is an example:
Once you have defined the string you can use it as you see fit. You can use it to change a control’s caption:
|
| These are functions performed on strings regardless of any other consideration. For example, before performing any operation on a string, sometimes you will first need to find out whether the string contains something or it is empty. Eventually, you will decide what to do if the string is empty.
The AnsiString class provides its own function used to check whether a string is empty. Its syntax is:
| This function can be used to check whether a text-based control contains nothing but it cannot empty a text control. The following example shows two ways of using the AnsiString::IsEmpty() method:
To remove any (empty) space on the left side of a string, you can use the AnsiString::TrimLeft() method. Its syntax is:
AnsiString __fastcall TrimLeft() const;
If the original string has space on its left, this function would remove it and return a string that is like the original without the leading space. If the original does not have any leading space, the function would return the same string:
AnsiString _fastcall TrimLeft(const AnsiString S);
| To remove any space on the right side of a string, you can use the AnsiString::TrimRight() method. Its syntax is:
If the original string has space on its right, this function would remove it and return the same string without any trailing space. Otherwise, the original string would be returned:
AnsiString _fastcall TrimRight(const AnsiString S);
| Other functions allow you to combine the last two operations into one. You can use the AnsiString::Trim() method to remove spaces on both sides of a string. Its syntax is:
Here is an example of using this method:
AnsiString _fastcall Trim (const AnsiString S);
| Text that the user types in a program, such as in an edit box is considered a string. This is because the compiler cannot assume what kind of value the user or the client of an Edit control would supply. For this reason, after a value has been provided to a control that uses the AnsiString as the basis of its content, if you want to perform any mathematical operation on the string you must convert the string to a valid data type.
The AnsiString provides a lot of constructors that allow you to create a string of any kind. For example you can use it to declare:
a character:
an interger
a long integer
a floating-point value:
a double-precision number:
a string
Based on the configurations of the AnsiString constructors, you can convert any value and make it available to a control that uses an AnsiString property. For example, you can convert and display:
| a character:
an interger
a long integer
a floating-point value:
a double-precision number:
a string
The AnsiString class is configured to recognize null-terminated strings of the classic C string functions. The AnsiString class has a constructor that can convert a null-terminated C string to AnsiString. Thanks to this constructor, the AnsiString(const char* Source), you can declare a C string and use it as you see fit:
| The AnsiString class does not allow you to request its variables or display its values on the DOS prompt. Alternatively, you can use C functions, such as gets() and puts(), as intermediary for this operation. This allows you to write a Console application that can still use AnsiString strings. Here is an example:
| To convert a lowercase character or string to uppercase, you can use the AnsiString::UpperCase() function. Its syntax is:
AnsiString __fastcall UpperCase() const;
This function considers an AnsiString variable and examines each one of its characters. If a character is an alphabetic character in lowercase, it would be converted to uppercase. If the character is either an alphabetical character in uppercase or it is not an alphabetic character, it would be kept “as is”. This method also considers the Regional Settings of the computer being used.
If you want to convert a single character to uppercase, after initializing or getting, call this method. Here is an example:
| This would produce:
Besides the the AnsiString method, you can use the UpperCase() function to convert a character or string to uppercase. Its syntax is:
AnsiString __fastcall UpperCase(const AnsiString S);
This function uses the same algorithm as the AnsiString::UpperCase() method. Here is an example of using it:
|
Besides the AnsiString::AnsiCompare() method, you can use the AnsiCompareStr() function to compare strings. Like the AnsiString::AnsiCompare() method, this method takes into consideration the Windows Regional Setings. Its syntax is:
int __fastcall AnsiCompare(const AnsiString& OtherString) const;
The function considers its own string and compares it to the string argument it takes. This function returns:
int __fastcall CompareStr(const AnsiString First, const AnsiString Second);
The function takes two string arguments and examines their characters incrementally. After the comparison, the function returns:
The AnsiString::AnsiCompareIC() method performs a comparison of two strings, considering the Regional Settings. Unlike the AnsiString::AnsiCompare() method, this function does not care about case-sensitivity.
1.2.3.
The AnsiString and the sysutils library provide techniques of comparing strings. The functions we have used to perform comparisons returned integral values at the end of their comparisons. Sometimes, when performing specific algorithms, such as comparing passwords, performing mathematical calculations, performing spell checks in text documents, etc, you will only need to know whether two strings are equal. This type of comparison renders a Boolean value of true or false. Both libraries tend to handle any sort of comparison.
You can also use the AnsiSameStr() function. Its syntax is:
bool __fastcall AnsiSameStr(const AnsiString First, const AnsiString Second);
The function takes the Windows Regional Settings into consideration when comparing the First and the Second strings with case-sensitivity. If both strings are the same, the function return true. If they are not the same, the result is false. Here is an example:
| The == operator is configured to compare two strings. The strings are typed on both sides of the operator. If both are the same, the comparison would return a true value:
| The operator is used to add two strings:
The = operator is used to assign one string to another;
| There are many operations you can perform on individual characters of an AnsiString variable. These include checking for a character, finding the position of a character, or deleting a character or characters.
If the AnsiString is used on a console application, you can use this same method to find the last character of an AnsiString variable:
| Sometimes you will want to get rid of a character in a string. This is done using the AnsiString::Delete() method. The syntax is:
AnsiString& __fastcall Delete(int Index, int Count);
This function takes two integer arguments. The first argument specifies the position where the compiler would start considering the deletion. The second argument specifies the number of characters that should be deleted from the AnsiString variable.
If you declare an AnsiString variable called Country. You can use Country.Delete(14, 11) to delete 11 characters starting at the 14th character:
| In the strict of string routines, a quote is a character or symbol used to delimite a string. It sets the beginning and end of a string. In the English language, a quote is represented with the double-quote symbol “. The VCL is equipped with functions used to insert or remove quotes from a string.
| When a string is provided with quotes and you want to remove the quotes, use the AnsiExtractQuotedStr() function. Its syntax is:
AnsiString __fastcall AnsiExtractQuotedStr(char * &Source, char Quote);
This function takes two arguments. The Source parameter is a null-terminated string that is returned as an AnsiString value. When using the function, you must specify what character or symbol is used as Quote. Here is an example:
| With a string, you can create a new string retrieved from the original using the AnsiString::SubString() method. Its syntax is:
AnsiString __fastcall SubString(int StartPosition, int HowManyChars) const;
This function takes two arguments as integers. From the original string, the first argument specifies the position of the character where the new string would start. The second argument specifies the number of characters that would be considered when creating the new string. Here is an example:
|
|
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit1->Text =
StringReplace(Edit1->Text, " ", "",
TReplaceFlags() << rfReplaceAll);
}
//---------------------------------------------------------------------------
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |