Você está na página 1de 10

String operations in C++ Builder are mainly performed using a class called AnsiString.

The AnsiString class is not derived from TObject. Therefore, it has a high level of
independence and flexibility from the application or the control that wants to use it.

It is simply incredible the level of work and support provided by the VCL to its strings
and text-related operations. Almost any possible operation that can be performed on a
string is supported. There are so many functions that we will review only those that are
most used in this book but all functions that were created in the libraries are highly
valuable and can save you a tremendous amount of code writing and headache.

Many controls use AnsiString properties. All controls that use a caption (forms, panels,
labels, etc) have their Caption property set as an AnsiString value. Many other controls
such as the edit box use the AnsiString class as the basis of their text. Based on these two
facts, you have already used and implemented AnsiString values. In some other scenarios
you will need to declare and possibly initialize a string before using it.

To declare a string, use the AnsiString word followed by a valid C++ name. Here is an
example:

AnsiString Country;

Since AnsiString is a class with its own constructor, you can also declare its variable with
empty parentheses, which would be calling the class constructor. Here is an example:

AnsiString City();

String Initialization

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:

AnsiString Country;
Country = Madagascar;

You can also initialize the string variable when declaring it, again, using the assignment
operator and providing the desired value. Here is an example:

AnsiString Province("British Columbia");

Once you have defined the string you can use it as you see fit. You can use it to change a
controls caption:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString Country;
Country = "Madagascar";
Panel1->Caption = Country;

wawan
}
//---------------------------------------------------------------------------

You can also use it to fill out an edit control:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString City = "Antananrivo";
Edit1->Text = City;
}
//---------------------------------------------------------------------------

General Purpose String Functions

String Emptiness

General purpose functions are those that perform on strings regardless of any other
consideration. For example, before performing any operation on a string, sometimes you
will need first to find out whether the string contains something or is empty. Eventually,
you will decide what to do if the string is empty.

There are two main ways you can check whether the content of a text-based control is
empty. You can just use the AnsiString ==overloaded operator. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString Original = Edit1->Text;

if(Original == "")
Edit2->Text = "Why is Edit1 empty?";
}
//---------------------------------------------------------------------------

The AnsiString class provides its own function used to check whether a string is empty.
Its syntax is:

bool __fastcall IsEmpty() const;


This member 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:

wawan
//---------------------------------------------------------------------------
void __fastcall TOKBottomDlg::OKBtnClick(TObject *Sender)
{
String UserName = edtUserName->Text;

if( Edit1.IsEmpty() )
Panel1->Caption = "Please provide a User Name";
if( Edit2->Text == "" )
Panel1->Caption = "You need to type a password";
if( Edit3->Text.IsEmpty() )
Panel1->Caption = "Your account is not complete";

edtUserName->SetFocus();
}
//---------------------------------------------------------------------------

The Length of a String

When a string has been initialized or at least is not empty, it has a length. There are
various ways you can get or control the length of a string.

To find the length of a string, if the string is from the C string class, you can first convert
it using the AnsiString::c_str() function, then use the strlen() function to get its length:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char* S = Edit1->Text.c_str();
Edit2->Text = strlen(S);
}
//---------------------------------------------------------------------------

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();
}
//---------------------------------------------------------------------------

The AnsiString class allows you to impose the number of characters of a string. It uses
the following method:

AnsiString& __fastcall SetLength(int NewLength);


wawan
This method takes one argument which is the length y

ou want the AnsiString variable to have. If the NewLength argument is less than the
length of the string, the resulting string would be the first NewLength characters of the
original string. If the NewLength value is less than the length of the stri

wawan
ng, the original would be preserved and assigned to the resulting string:

//---------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString S = Edit1->Text;
Edit2->Text = S.SetLength(7);
}
//---------------------------------------------------

//---------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString S = Edit1->Text;
Edit2->Text = S.SetLength(4);
}
//---------------------------------------------------

String Trimming

Trimming a string is an operation that gets rid of leading or ending spaces in a string. 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;

wawan
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();
}
//---------------------------------------------------------------------------
Another function used to perform the same operation is the TrimLeft(). Its syntax is:

AnsiString _fastcall TrimLeft(const AnsiString S);

As opposed to the AnsiString::TrimLeft() method, the (global) TrimLeft() function takes


one argument which is the string

Você também pode gostar