Table of contents
1.
Introduction
2.
Syntax
3.
Public Constructors
4.
Public Methods
5.
Public Operators
6.
Requirements
7.
CStringT::AllocSysString
8.
CStringT::Compare
9.
CStringT::CompareNoCase
10.
CStringT::Delete
11.
CStringT::MakeLower
12.
CStringT::operator =
13.
Frequently Asked Questions
13.1.
What is a utility class?
13.2.
What is ATL?
13.3.
What is MFC?
13.4.
What are the classes shared by MFC and ATL?
14.
Conclusion
Last Updated: Mar 27, 2024
Medium

UtilityClass-CStringT

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

CStringT is a class in the ATL(Active Template Library). The ATL(Active Template Library) is a set of template-based C++ classes. CStringT class is used to track the string length for faster performance.

intro image

In this article, we will discuss what is UtilityClass-CStringT, so let’s get started with it.

Syntax

The syntax of CStringT is as follows,

template<typename BaseType, class StringTraits>
class CStringT:
    public CSimpleStringT<BaseType,
        _CSTRING_IMPL_::_MFCDLLTraitsCheck<BaseType, StringTraits>::c_bIsMFCDLLTraits>

Public Constructors

Name Description
CStringT::CStringT Constructs a CStringT object.
CStringT::~CStringT Destroys a CStringT object.

Public Methods

Name of the method Description
CStringT::AllocSysString It allocates a BSTR from CStringT data.
CStringT::AnsiToOem It makes an in-place conversion from the ANSI character set to the OEM character set.
CStringT::AppendFormat We use this method to append formatted data to an existing CStringT object.
CStringT::Collate It compares two strings. It is case-sensitive and uses locate-specific information.
CStringT::CollateNoCase This method compares two strings. It is case-insensitive and uses locate-specific information.
CStringT::Compare It compares two strings. It is case-sensitive.
CStringT::CompareNoCase This method compares two strings. It is case-insensitive.
CStringT::Delete We use this method to delete character(s) from a string.
CStringT::Find We use this method to find a substring or a single character in a larger string.
CStringT::FindOneOf This method finds a character in a string and gives the first matching from its set.
CStringT::Format It formats the string.
CStringT::FormatMessage It formats a message string.
CStringT::FormatMessageV It formats a message string using a variable list of arguments.
CStringT::FormatV It formats the string using a variable argument list.
CStringT::GetEnvironmentVariable

This method sets the string to the value of the specified environment variable.

 

CStringT::Insert We use this method to insert a substring or a single character in a particular index.
CStringT::Left It extracts the left part of the string.
CStringT::LoadString

We use this method to load an existing CStringT object from a Windows resource.

 

CStringT::MakeLower It makes the string lowercase by converting all the characters to lowercase.
CStringT::MakeReverse To reverse the string, we use this string.
CStringT::MakeUpper It makes the string uppercase by converting all the characters to uppercase.
CStringT::Mid It gives us the middle part of the string.
CStringT::OemToAnsi This method converts the OEM character set to the ANSI character set in place.
CStringT::Remove This method removes character(s) from the string.
CStringT::Replace To replace character(s), we use this method.
CStringT::ReverseFind

.To find a character in a larger string, we use this method. But it starts from the end.

 

CStringT::Right It extracts the right part of the string.
CStringT::SetSysString We use this method to set a BSTR object with data from a CStringT object.
CStringT::SpanExcluding This method extracts characters from the string, which is not in the set of characters identified by pszCharSet. It starts with the first character.
CStringT::SpanIncluding

We use this method to Extract a substring that contains only the characters in a set.

 

CStringT::Tokenize It extracts specified tokens in a target string.
CStringT::Trim This method trims all the leading and trailing whitespace characters from the string.
CStringT::TrimLeft We use this method to trim only the leading whitespace characters from the string.
CStringT::TrimRight We use this method to trim only the trailing whitespace characters from the string.

Public Operators

Name of the operator Description
CStringT::operator = We Assign a new value to a CStringT object with this operator.
CStringT::operator + To add a character and a string or concatenate two strings.
CStringT::operator += This method concatenates a new string to a string at the end of it.
CStringT::operator == Checks if the two strings are equal.
CStringT::operator != Checks if two strings aren’t equal.
CStringT::operator < Determines if the left side string is less than the right side string.
CStringT::operator > Determines if the left side string is greater than the right side string.
CStringT::operator <= Determines if the left side string is less than or equal to the right side string.
CStringT::operator >= Determines if the left side string is greater than or equal to the right side string.

Requirements

The required headers are

  • For MFC-only string objects cstringt.h.
     
  • For Non-MFC string objects atlstr.h.

CStringT::AllocSysString

This method allocates an automation-compatible string of BSTR type. Then it copies the CStringT object into it.

Code

/* typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString;*/
CAtlString str(_T("Hello World!"));
BSTR b_s_t_r = str.AllocSysString();

/* b_s_t_r now contains "Hello World!" and can be */
/* passed to any OLE function requiring a BSTR.*/
/* If you pass the BSTR, you */
/* need to free the string after returning from the function call.*/


Note: This public method(CStringT::AllocSysString) returns the newly allocated string.

CStringT::Compare

This method compares two strings, and it is case-sensitive.

Code

/* typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString;*/

 
CAtlString string1(_T("abc"));
CAtlString string2(_T("abd"));

/* Compare with another CAtlString. */
ASSERT(string1.Compare(string2) < 0); 

/* Compare with LPTSTR string. */
ASSERT(string1.Compare(_T("abe")) < 0); 


Note: It returns zero for identical strings. For a CStringT object less than psz(other string for comparison), it returns a negative value; for a CStringT object greater than psz, it returns a positive value.

CStringT::CompareNoCase

This method compares two strings, and it is case-insensitive.

Code

/*typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString;*/

 
CAtlString string1(_T("abc"));
CAtlString string2(_T("ABD"));

 /* Compare with a CAtlString. */
ASSERT(string1.CompareNoCase(string2) < 0);

/* Compare with LPTSTR string. */
ASSERT(string1.CompareNoCase(_T("ABE")) < 0); 


Note: It returns zero for identical strings. For a CStringT object less than psz(other string for comparison), it returns a negative value; for a CStringT object greater than psz(case-insensitive), it returns a positive value.

CStringT::Delete

This method deletes a character(s) from a string starting with the character at the given index.

Code

/* typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString;*/

 
CAtlString str(_T("Coding Ninjas is the best platform to learn to code."));
_tprintf_s(_T("Before: %s\n"), (LPCTSTR)str);

 
int n = str.Delete(25, 9);
_tprintf_s(_T("After %s\n"), (LPCTSTR)str);
ASSERT(n == str.GetLength());


Output:

Before: Coding Ninjas is the best platform to learn to code.
After: Coding Ninjas is the best to learn to code.


Note: It returns the length of the changed string.

CStringT::MakeLower

It makes the string lowercase by converting all the characters to lowercase.

Code

/* typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString; */
 
CAtlString str(_T("ABC")); 
ASSERT(str.MakeLower() == _T("abc"));

CStringT::operator =

We Assign a new value to a CStringT object with this operator.

Code

CStringT& operator=(const CStringT& strSrc);
 
template<bool bMFCDLL>
CStringT& operator=(const CSimpleStringT<BaseType, bMFCDLL>& str);
CStringT& operator=(const unsigned char* pszSrc);
CStringT& operator=(const VARIANT& var);
 
/*Note -*/
/*bMFCDLL is a boolean specifying whether the project is an MFC DLL or not.*/
/*pszSrc is a pointer to the original string being assigned.*/


Explanation: The assignment operator accepts another CStringT object, a single character, or a character pointer. During this operation, we allocate new storage so that memory exceptions can occur.

Alright! Now we hope you understand the UtilityClass-CStringT.

Also check out - Substr C++

Frequently Asked Questions

What is a utility class?

A utility class is a class that has only static methods that perform certain operations on the objects passed as parameters. Utility classes generally have no state.

What is ATL?

ATL stands for the Active Template Library. ATL is a set of template-based C++ classes developed by Microsoft and intended to simplify the programming of Component Object Model objects.

What is MFC?

MFC stands for Microsoft Foundation Class Library. MFC is a C++ object-oriented library for developing desktop applications for Windows. MFC was introduced by Microsoft in 1992 and quickly gained widespread use.

What are the classes shared by MFC and ATL?

The classes shared by MFC and ATL are CFileTime, CFileTimeSpan, CFixedStringT, CImage, COleDateTime, COleDateTimeSpan, CPoint, CRect, CSimpleStringT, CSize, CStrBufT, CStrBufT, CStringT, CTime, CTimeSpan, and IAtlStringMgr.

Conclusion

In this article, we discussed the UtilityClass-CStringT. We hope this blog on UtilityClass-CStringT was helpful. You can also refer to other similar articles.


You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning Ninja!

Live masterclass