Zh/String: Difference between revisions

From Valve Developer Community
< Zh
Jump to navigation Jump to search
mNo edit summary
m (Undo revision 238964 by HouJunhao33 (talk))
Tag: Undo
Line 1: Line 1:
{{lang}}
{{otherlang2
|en=String
|de=String:de
}}
{{toc-right}}
{{toc-right}}



Revision as of 03:54, 16 January 2021

Template:Otherlang2

charASCII)或 wchar_tUnicode)的数组被经常用来储存文本。这些缓冲区有特定的术语: 字符串,或有时被称为“C语言式字符串”。它们很重要,但也比较复杂。

Null 终结符

字符串总是要比其文本内容多一个字符,这多的一个字符就是 null 终结符(二进制上的 0)。字符串需要终结符,是因为字符串经常用指针来传递,而指针并不像数组那样自带长度信息。

若没有终结符,就无法判断字符串结尾的位置。因此会读取到下一个变量或者是未分配内存的信息,这就造成了缓冲区溢出,大大滴坏!

Tip.png提示:Null 终结符不需要位于字符串最后,字符串处理函数会忽略掉 Null 终结符后的内容。

从字面量创建字符串

char* MyString = "Hello world"; // 必须赋值给指针!

This code creates a string from a string literal. The double quote marks are special syntax which generate an array of char from their contents. The code above therefore:

  1. Assigns 12 bytes of memory, somewhere arbitrary, to store the string. This is one byte for each character, plus an automatic twelfth for the null terminator.
  2. Creates a local char pointer containing the address of the first character (H in this case).

String literals are often handed with const variables. This is because a string literal will stay in memory for the process's whole lifespan.

Note.png注意:If you examine MyString in the Visual Studio debugger, you will see the entire string. This is special behaviour to make examining strings easier; strictly speaking it should just show you the pointee (i.e. the first character, H).

Creating by size

char MyString[12]; // accepts a static size only

int StringLen = 12;
char* pMyString = new char[StringLen]; // accepts a variable size

delete[] pMyString; // always delete / delete[] anything created with 'new' after use

This code both allocates two 12-byte strings, but does not write anything to them (so their contents will be either blank or gibberish). They need to be assigned to, ideally with a string function like strcpy() or sprintf().

The difference between them is that one creates an array in the function's memory space, while the other creates a pointer in the function and uses new to allocate the string itself somewhere else. The advantage of new is that you can allocate an array of a size determined at run-time, but the downside is that if you aren't scrupulous about calling delete (or delete[] for arrays) you will suffer a memory leak.

Unicode strings

Note.png注意:Source is internally ASCII. The only time you will deal with Unicode is if you delve into the inner workings of VGUI.

Unicode strings behave similarly to ASCII strings, but are instead arrays of wchar_t. They are operated on by their own set of string functions, normally with 'wc' or 'wcs' (wide char string) in their name.

wchar_t* MyWideString = L"Здравей свят";

The L marks the string literal as being Unicode. You need to do this even if the characters are all ASCII-compatible.

String functions

There are a multitude of functions which process strings, of which the most common ASCII variants have Source-specific V_* equivalents. See MSDN for a quite comprehensive list, or search VS' Class View for "V_str".