全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1242
推到 Plurk!
推到 Facebook!

A Study of VCL Strings

 
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-07-12 16:39:50 IP:61.219.xxx.xxx 未訂閱
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:

The AnsiString class was redefined as String in the VCL documentation (actually, you can find its typedef redefinition in the sysmac.h header file). Therefore, instead of AnsiString, you can use the word String wherever you would use the AnsiString word.


AnsiString Country;
Country = “Madagascar”;
Once you have defined the string you can use it as you see fit. You can use it to change a control’s caption:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString Country;
Country = "Madagascar";
Panel1->Caption = Country;
}
//---------------------------------------------------------------------------
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:
//---------------------------------------------------------------------
void __fastcall TOKBottomDlg::OKBtnClick(TObject *Sender)
{
String UserName = edtUserName->Text;
if( UserName.IsEmpty() )
Panel1->Caption = "Please provide a User Name";
if( edtPassword1->Text == "" )
Panel1->Caption = "You need to type a password";
if( edtPassword2->Text.IsEmpty() )
Panel1->Caption = "Your account is not complete";
edtUserName->SetFocus();
}
//---------------------------------------------------------------------------
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:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit2->Text = Edit1->Text.TrimLeft();
}
//---------------------------------------------------------------------------
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:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit2->Text = Edit1->Text.TrimRight();
}
//---------------------------------------------------------------------------
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:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit2->Text = Edit1->Text.Trim();
}
//---------------------------------------------------------------------------
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:
AnsiString Symbol = 'H';
an interger
AnsiString Int = 120;
a long integer
AnsiString Longer = -73495745;
a floating-point value:
AnsiString WeeklyEarnings = 675.15;
a double-precision number:
AnsiString WeeklyEarnings = 675.15;
AnsiString Silver = 2.15e28;
a string
AnsiString GirlFriend = "Micheline Phoon";
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:
char Sign = 'q';
Edit1->Text = AnsiString(Sign);
an interger
Integer Number = 808;
Caption->Text = AnsiString(Number);
a long integer
long Value = 497783L;
Panel1->Caption = AnsiString(Value);
a floating-point value:
Float Distance = 1205.62;
Label1->Caption = AnsiString(Distance);
a double-precision number:
Double YearlyIncome = 24588;
Edit1->Text = AnsiString(YearlyIncome);
a string
AnsiString Food = "Peanut Butter";
Button2->Caption = AnsiString(Food);
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:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String S1 = "James N. Fame!";
String S2 = S1.UpperCase();
Edit1->Text = S2;
}
//---------------------------------------------------------------------------
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:
//-------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String S1 = "La maison? Ça a été brûlée!";
String S2 = AnsiUpperCase(S1);
Edit2->Text = S2;
}
//-------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::btnConvertClick(TObject *Sender)
{
String String1 = "[Borland C Builder]";
edtConvert->Text = String1.LowerCase();
}
//---------------------------------------------------------------------------
You can also use the LowerCase() function to convert a character or string to lowercase. Its syntax is:
AnsiString __fastcall LowerCase(const AnsiString S);
This function uses the same algorithm as the AnsiString::UpperCase() method. Here is an example of using it:
//---------------------------------------------------------------------------
void __fastcall TForm1::btnConvertClick(TObject *Sender)
{
String S1 = "La version Française de Borland C Builder est là. "
"Elle est arrivée!";
edtConvert->Text = AnsiLowerCase(S1);
}
//---------------------------------------------------------------------------
//---------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString S = Edit1->Text;
Edit2->Text = S.SetLength(7);
}
//---------------------------------------------------
To add one AnsiString object to another, you use the addition operator (it was overloaded to respond appropriately). The operation would produce a new string that combines the first and the second string. Here is an example:
1.2.3.4.
Double-click the edtFirstName control to launch its Onchange event. Implement it as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::edtFirstNameChange(TObject *Sender)
{
edtFullName->Text = edtFirstName->Text " "
edtLastName->Text;
}
//---------------------------------------------------------------------------
Also implement the OnChange event of the edtLastName as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::edtLastNameChange(TObject *Sender)
{
edtFullName->Text = edtFirstName->Text " "
edtLastName->Text;
}
//---------------------------------------------------------------------------
Double-click the panel object to initiate its OnClick event. Implement its OnClick event with Close();
To test the application, on the Debug toolbar, click the Run button.
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString Destination = "Paul ";
AnsiString Source = "Lombard";
AppendStr(Destination, Source);
Edit1->Text = Destination;
}
//---------------------------------------------------------------------------
There are various methods you can use to get or control the length of a string.
To get the length of an AnsiString variable, use the AnsiString::Length() method. Its syntax is:
int __fastcall Length() const;
This function returns the length of the string as an integer. Here is an example:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString S = Edit1->Text;
Edit2->Text = S.Length();
}
//---------------------------------------------------------------------------
AnsiString& __fastcall SetLength(int NewLength);
The SameText() function is used to compare two strings. Its syntax is:
bool __fastcall SameText(const AnsiString String1, const AnsiString String2);
The function requires two AnsiString variables and compares them. The comparison is performed without case-sensitivy. If the strings are the same, the result is true (the integer equivalent is 1); otherwise the comparison yields false (0).
You can use the SameText() function on a validation dialog like this one:
//---------------------------------------------------------------------
void __fastcall TOKBottomDlg::OKBtnClick(TObject *Sender)
{
String Password1 = edtPassword1->Text;
String Password2 = edtPassword2->Text;
Boolean Result = SameText(Password1, Password2);
if(Result == False)
{
Panel1->Caption = "Your passwords do not match!";
edtPassword1->SetFocus();
}
else
{
Panel1->Caption = "Your account has been setup.";
Close();
}
}
//---------------------------------------------------------------------------
The AnsiString::AnsiCompare() method is used to compare two strings with regard to case sensitivity. This function, when performed on dates and currency values, considers the Regional Settings of your computer. Here is an example:
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:
    To compare strings, you can also use the CompareStr() function. Unlike the AnsiString::AnsiCompare() method, this function does not care about the Windows Regional Settings. The syntax of this function is:
    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.
      //---------------------------------------------------------------------------
      void __fastcall TForm1::Button1Click(TObject *Sender)
      {
      AnsiString String1 = Edit1->Text;
      AnsiString String2 = Edit2->Text;
      if(String1.AnsiCompareIC(String2) < 0)
      Edit3->Text = "True";
      else if(String1.AnsiCompareIC(String2) > 0)
      Edit3->Text = "False";
      else
      Edit3->Text = "Equal";
      }
      //---------------------------------------------------------------------------
      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:
      //---------------------------------------------------------------------------
      void __fastcall TForm1::btnSendClick(TObject *Sender)
      {
      String EMail1 = edtEMail1->Text;
      String EMail2 = edtEMail2->Text;
      if(AnsiSameStr(EMail1, EMail2))
      {
      frmCongratulations->ShowModal();
      Close();
      }
      }
      //---------------------------------------------------------------------------
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:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(Edit1->Text == Edit2->Text)
Panel1->Caption = "Same Text";
else
Panel1->Caption = "Different Stuff";
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String S1 = Edit1->Text;
String S2 = Edit2->Text;
if(S1 < S2)
Panel1->Caption = "The first is less than the second strings";
else
Panel1->Caption = "I don't understand this!";
}
//---------------------------------------------------------------------------
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.
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String S = Edit1->Text;
Edit2->Text = S.AnsiLastChar();
}
//---------------------------------------------------------------------------
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.
//---------------------------------------------------------------------------
void __fastcall TForm1::edtQuotedExit(TObject *Sender)
{
char *BookTitle = edtQuoted->Text.c_str();
AnsiString Quoted = QuotedStr(BookTitle);
edtBookTitle->Text = Quoted;
}
//---------------------------------------------------------------------------
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:
//---------------------------------------------------------------------------
#include
#include
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
AnsiString FirstName("Suzanne");
AnsiString LastName("ZOO");
AnsiString FullName = FirstName " " LastName;
puts(FirstName.c_str());
puts(LastName.c_str());
puts(FullName.c_str());
puts("\nPress any key to continue...");
getchar();
return 0;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit1->Text =
StringReplace(Edit1->Text, " ", "",
TReplaceFlags() << rfReplaceAll);
}
//---------------------------------------------------------------------------
註: 資料來源已不可考.
系統時間:2024-04-28 15:33:23
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!