Użycie CStringUsing CString
W tematach w tej sekcji opisano, jak programować za pomocą programu CString
.The topics in this section describe how to program with CString
. Aby uzyskać dokumentację referencyjną dotyczącą CString
klasy, zobacz dokumentację dotyczącą CStringT.For reference documentation about the CString
class, see the documentation for CStringT.
Aby użyć CString
, Dołącz atlstr.h
nagłówek.To use CString
, include the atlstr.h
header.
CString
Klasy, CStringA
i CStringW
są specjalizacjami szablonu klasy o nazwie CStringT w oparciu o typ obsługiwanych przez nich danych znakowych.The CString
, CStringA
, and CStringW
classes are specializations of a class template called CStringT based on the type of character data they support.
CStringW
Obiekt zawiera wchar_t
Typ i obsługuje ciągi Unicode.A CStringW
object contains the wchar_t
type and supports Unicode strings. CStringA
Obiekt zawiera char
Typ i obsługuje ciągi jednobajtowe i wielobajtowe (MBCS).A CStringA
object contains the char
type, and supports single-byte and multi-byte (MBCS) strings. CString
Obiekt obsługuje char
Typ lub wchar_t
Typ, w zależności od tego, czy symbol MBCS lub symbol Unicode jest zdefiniowany w czasie kompilacji.A CString
object supports either the char
type or the wchar_t
type, depending on whether the MBCS symbol or the UNICODE symbol is defined at compile time.
CString
Obiekt zachowuje dane znakowe w CStringData
obiekcie.A CString
object keeps character data in a CStringData
object. CString
akceptuje ciągi w stylu C o wartości NULL.CString
accepts NULL-terminated C-style strings. CString
śledzi długość ciągu w celu zwiększenia wydajności, ale zachowuje także znak NULL w danych przechowywanych znaków do obsługi konwersji do LPCWSTR.CString
tracks the string length for faster performance, but it also retains the NULL character in the stored character data to support conversion to LPCWSTR. CString
uwzględnia terminator wartości null podczas eksportowania ciągu w stylu C.CString
includes the null terminator when it exports a C-style string. Można wstawić wartość NULL w innych lokalizacjach w CString
, ale może to spowodować nieoczekiwane wyniki.You can insert a NULL at other locations in a CString
, but it may produce unexpected results.
Następujący zestaw klas ciągów może być używany bez konsolidacji biblioteki MFC z obsługą CRT lub bez niej: CAtlString
, CAtlStringA
, i CAtlStringW
.The following set of string classes can be used without linking an MFC library, with or without CRT support: CAtlString
, CAtlStringA
, and CAtlStringW
.
CString
jest używany w projektach natywnych.CString
is used in native projects. W przypadku projektów kodu zarządzanego (C++/CLI) Użyj System::String
.For managed-code (C++/CLI) projects, use System::String
.
Aby dodać więcej możliwości niż CString
, CStringA
lub CStringW
obecnie oferta, należy utworzyć podklasę zawierającą CStringT
dodatkowe funkcje.To add more capabilities than CString
, CStringA
, or CStringW
currently offer, you should create a subclass of CStringT
that contains the additional features.
Poniższy kod pokazuje, jak utworzyć CString
a i wydrukować go do wyjścia standardowego:The following code shows how to create a CString
and print it to standard output:
#include <atlstr.h>
int main() {
CString aCString = CString(_T("A string"));
_tprintf(_T("%s"), (LPCTSTR) aCString);
}
W tej sekcjiIn This Section
Podstawowe operacje CStringBasic CString Operations
Opisuje CString
operacje podstawowe, w tym tworzenie obiektów z ciągów literałów języka C, uzyskiwanie dostępu do pojedynczych znaków w CString
, łączenie dwóch obiektów i porównywanie CString
obiektów.Describes basic CString
operations, including creating objects from C literal strings, accessing individual characters in a CString
, concatenating two objects, and comparing CString
objects.
Ciąg Zarządzanie danymiString Data Management
Omawia użycie standardu Unicode i MBCS z CString
.Discusses using Unicode and MBCS with CString
.
Semantyka CStringCString Semantics
Wyjaśnia, jak CString
obiekty są używane.Explains how CString
objects are used.
Operacje CString odnoszące się do ciągów w stylu CCString Operations Relating to C-Style Strings
Opisuje manipulowanie zawartością CString
obiektu, taki jak ciąg w stylu C zakończony znakiem null.Describes manipulating the contents of a CString
object like a C-style null-terminated string.
Przydzielanie i zwalnianie pamięci dla typu BSTRAllocating and Releasing Memory for a BSTR
W tym artykule omówiono użycie pamięci dla obiektów BSTR i COM.Discusses using memory for a BSTR and COM objects.
CString — oczyszczanie wyjątkówCString Exception Cleanup
Wyjaśnia, że jawne czyszczenie w MFC 3,0 i nowszych nie jest już konieczne.Explains that explicit cleanup in MFC 3.0 and later is no longer necessary.
Przekazywanie argumentu CStringCString Argument Passing
Wyjaśnia, jak przekazać obiekty CString do funkcji i jak zwracać CString
obiekty z funkcji.Explains how to pass CString objects to functions and how to return CString
objects from functions.
Obsługa zestawu znaków Unicode i wielobajtowego (MBCS)Unicode and Multibyte Character Set (MBCS) Support
W tym artykule omówiono, w jaki sposób MFC jest włączone dla obsługi standardu Unicode i MBCS.Discusses how MFC is enabled for Unicode and MBCS support.
DokumentacjaReference
CStringTCStringT
Zawiera informacje referencyjne dotyczące CStringT
klasy.Provides reference information about the CStringT
class.
Klasa CSimpleStringTCSimpleStringT Class
Zawiera informacje referencyjne dotyczące CSimpleStringT
klasy.Provides reference information about the CSimpleStringT
class.
Sekcje pokrewneRelated Sections
Ciągi (ATL/MFC)Strings (ATL/MFC)
Zawiera łącza do tematów, które opisują kilka sposobów zarządzania danymi ciągu.Contains links to topics that describe several ways to manage string data.