You are on page 1of 12

else

{
Panel1->Caption = "Your account has been setup.";
Close();
}
}
//---------------------------------------------------------------------------

The AnsiString::AnsiCompareIC() method performs a comparison of two strings,


considering the Regional Settings. Like SameText(), 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";
}
//---------------------------------------------------------------------------

Alternatively, you can use the CompareText() function to compare strings. Unlike the
AnsiString::AnsiCompareIC() method, this function does not care about the Windows
Regional Settings. The syntax of this function is:

int __fastcall CompareText(const AnsiString First, const AnsiString Second);

The function takes two string arguments and examines their characters incrementally.
After the comparison, the function returns:

* a negative value if the First string is less than the Second


* a positive value if the First string is greater than the Second
* 0 if both strings are the same
String Comparison With Case-Sensitivity

The AnsiString::AnsiCompare() method is used to compare two strings with regards to


case sensitivity. This function, when performed on dates and currency values, considers
the Regional Settings of the user�s computer. Here is an example:

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

if(String1.AnsiCompare(String2) < 0)
Edit3->Text = "True";
else if(String1.AnsiCompare(String2) > 0)
Edit3->Text = "False";
else
Edit3->Text = "Equal";
}
//---------------------------------------------------------------------------

Besides the AnsiString::AnsiCompare() method you can use the AnsiCompareStr()


function to compare strings. Like the AnsiString::AnsiCompare() method, this function
takes into consideration the Windows Regional Settings. 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:

* a negative value if your string is less than the OtherString


* a positive value if your string is greater than the OtherString
* 0 if both strings are the same

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:
* A negative value if the First string is less than the Second
* A positive value if the First string is greater than the Second
* 0 if both strings are the same

Strings Boolean Comparisons

The AnsiString class 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 can perform any sort
of comparison.

When you have two strings and would like to find out whether both are equal, you can
use the (overloaded) == operator. If both strings are equal, the conditional comparison
would return true.

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

Alternatively, to compare two strings, you can use the AnsiSameText() function. Both
functions use the same syntax. Like the AnsiSameStr() function, the AnsiSameText()
considers the Regional Settings. Unlike the AnsiSameStr() function, the AnsiSameText()
function does not consider the case of the characters in the strings.
If the strings you want to compare are captions, such as those you see on a form, it would
be cumbersome to write a comparison function that would examine them. This is because
the captions on a form usually have an ampersand used to underline one of their
characters. Examples include First Name, Address, City, Full Name or Department.
Fortunately, Borland provides the AnsiSameCaption() function. Its syntax is:

bool __fastcall AnsiSameCaption(const AnsiString First, const AnsiString Second);

This function takes two captions and compares them considering the Regional Settings .
Regardless of where the ampersand is positioned, the other characters of the captions
would be examined. If both captions are the same, the function would return true. In the

Wawan
weding
following example, two captions are set as First Name and First Name respectively. A
regular comparison would find them different, but the AnsiSameCaption() function finds
that both strings are the same:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
lblCaption1->Caption = "&First Name";
lblCaption2->Caption = "First &Name";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCompareCaptionsClick(TObject *Sender)
{
String Caption1 = lblCaption1->Caption;
String Caption2 = lblCaption2->Caption;
if(AnsiSameCaption(Caption1, Caption2))
Panel1->Caption = "Same captions";
else
Panel1->Caption = "Completely Different";
}
//---------------------------------------------------------------------------

Besides all available comparison methods of the AnsiString class and comparison
functions throughout the VCL, you can use the Boolean operators to perform any type of
comparisons on strings. These operators can be used the same way you would proceed
with regular numeric variables. Therefore, the operators are:

* Equal: ==
* Not Equal: !=
* Less Than <
* Less Than Or Equal To <=
* Greater Than >
* Greater Than Or Equal To >=

Characters and Sub-Strings

The Last Character of a String


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. These operations can be valuable when creating objects
such as login dialog boxes.

To find the last character of a string, use the AnsiString::AnsiLastChar() method. Its
syntax is:

char* __fastcall AnsiLastChar() const;

You can use this method to find out the last character of a given string. In the following
example, the last character of the string in the Edit1 edit box displays in the Edit2 edit
box when the user clicks the Button1 control:

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

Wawan
weding
If the AnsiString is used on a console application, you can use this same method to find
the last character of an AnsiString variable.

Character Deletion
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 member 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:

AnsiString Country("United States of America");


AnsiString After;
puts(Country.c_str());
After = Country.Delete(14, 11);
puts(After.c_str());

Sub-String Creation

A sub string is a string that is created from, or is included in, another string. C++ and C+
+ Builder allow you to find a sub string in an original string, to get the position of a sub
string in a string, etc.

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 method takes two integer arguments. 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)
{
AnsiString Original = Edit1->Text;
AnsiString SubOne = Original.SubString(9, 4);
Edit2->Text = SubOne;
}
//---------------------------------------------------

The Position of a Sub String


The AnsiString class allows you to analyze a string and find out whether it contains a
certain sub string. If it does, you can get the position of the substring, using the
AnsiString::Pos() method. Its syntax is:

int __fastcall Pos(const AnsiString& SubString) const;

In many cases, you can also use the AnsiString::AnsiPos() method). Its syntax is:

extern PACKAGE int __fastcall AnsiPos(const AnsiString Substr, const AnsiString S);

Character or Sub-String Replacement

When performing your algorithms or other specific types of operations on strings, you
may want to find out whether a certain character or group of characters has been provided
in a string. If so, you may want to replace it with a different character or with a new sub
string. This operation is handled by the StringReplace() function. Its syntax is:

AnsiString __fastcall StringReplace(const AnsiString Original,


const AnsiString LookFor,
const AnsiString ReplaceWith,
TReplaceFlags FlagToUse);

The StringReplace() function will look for the LookFor character or sub-string in the
Original string. If it finds it, then it will replace the LookFor character or sub string with
the ReplaceWith character or sub-string. You control how this operation is performed by
using the FlagToUse argument. The values you can use are to replace all occurrences of
LookFor with ReplaceWith. The flag used would be rfReplaceAll. You can also ask the
compiler not to take the character(s) case into consideration, which is done with
rfIgnoreCase. Once the TReplaceFlags argument is a set, you can use one or both of the
flags. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit1->Text = StringReplace(Edit1->Text, " ", "",
TReplaceFlags() << rfReplaceAll);
}
//---------------------------------------------------------------------------

String Quotations

Regular String to Quoted String Conversion


In the strict of string routines, a quote is a character or symbol used to delimit 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.

The AnsiQuotedStr() function is used to �convert� a string into a quoted string. Its
syntax is:

AnsiString __fastcall AnsiQuotedStr(const AnsiString Source, char Quote);

This function takes one string, the Source argument, and returns it added the Quote
characters on both sides of the string. Here is an example:
Wawan
weding

You might also like