P. 1
Programing - Learn Visual C++ 6

Programing - Learn Visual C++ 6

|Views: 130|Likes:
Published by HitMAN010

More info:

Published by: HitMAN010 on Feb 20, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

10/08/2012

pdf

text

original

There are few programs that don't have to deal with text strings of one sort or
another. Unfortunately, C++ is infamous for its weak string-handling capabilities,
whereas languages such as BASIC and Pascal have always enjoyed superior power when
it comes to these ubiquitous data types. MFC's CString class addresses C++'s string
problems by providing member functions that are as handy to use as those found in other
languages. Table F.4 lists the commonly used member functions of the CString class.

Table F.4 Commonly Used Member Functions of the CString Class

Function

Description

Compare()

A case-sensitive compare of two strings

CompareNoCase()Not a case-sensitive compare of two strings

Empty()

Clears a string

Find()

Locates a substring

Format()

"Prints" variables in a CString much like the C sprintf
function

GetAt()

Gets a character at a specified position in the string

GetBuffer()

Gets a pointer to the string's contents

GetLength()

Gets the number of characters in the string

IsEmpty()

Returns TRUE if the string holds no characters

Left()

Gets a string's left segment

MakeLower()

Lowercases a string

MakeReverse()

Reverses a string's contents

MakeUpper()

Uppercases a string

Mid()

Gets a string's middle segment

Right()

Gets a string's right segment

SetAt()

Sets a character at a specified position in the string

TrimLeft()

Removes leading whitespace characters from a string

TrimRight()

Removes trailing whitespace characters from a string

Besides the functions listed in the table, the CString class also defines a full set of
operators for dealing with strings. Using these operators, you can do things like
concatenate (join together) strings with the plus sign (+), assign values to a string object
with the equal sign (=), access the string as a C-style string with the LPCTSTR operator,
and more.

Creating a string object is quick and easy, like this:

CString str = "This is a test string";

Of course, there are lots of ways to construct your string object. The previous example
is only one possibility. You can create an empty string object and assign characters to it
later, you can create a string object from an existing string object, and you can even
create a string from a repeating character. The one thing you don't have to do is decide
the size of your string as you make it. Managing the memory isn't your problem any more.

After you have created the string object, you can call its member functions and
manipulate the string in a number of ways. For example, to convert all the characters
in the string to uppercase, you'd make a function call like this:

str.MakeUpper();

To lengthen a string, use the + or += operators, like this:

CString sentence = "hello " + str;
sentence += " there."

To compare two strings, you'd make a function call like this:

str.Compare("Test String");

You can also compare two CString objects:

CString testStr = "Test String";
str.Compare(testStr);

or neater still:

if (testStr == str)

If you peruse your online documentation, you'll find that most of the other CString
member functions are equally easy to use.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->