📄 article3.htm
字号:
</FONT></PRE><P><h4>String Indexing </H4><P>The String class provides an indexing operator to insert or extract characters in strings. For example: <P><PRE><FONT FACE="COURIER" SIZE="2">// sWide=="Wide", i==2, wch=='n'
sWide[i] = wch; // sWide=="Wine"
wch = sWide[i - 1]; // wch=='i'
sWide[0] = 'F'; // sWide=="Fine"
</FONT></PRE><P>There's nothing to prevent you from enhancing the index operator so that you could insert a string with it or even extract one. I'll leave that to you. <P><h4>Concatenation</H4><P>Any string type worth its salt must be capable of concatenation, and String does it as you would expect--with the + and += operators. It can append characters or strings:
<P><PRE><FONT FACE="COURIER" SIZE="2">// sChar=="A", sIn=="Send me in"
sChar += sIn; // sChar=="ASend me in"
sChar += WCHAR('F'); // sChar=="ASend me inF"
sChar += 'G'; // sChar=="ASend me inFG"
sChar += _W("Wide"); // sChar=="ASend me inFGWide"
sChar += "Narrow"; // sChar=="ASend me inFGWideNarrow"
sTmp = sNarrow + sNative + _W("Slow") + "Fast" + WCHAR('C') + 'D'
// sTmp=="NarrowNativeSlowFastCD"
</FONT></PRE><P>Some of the String methods look and act like Visual Basic string functions. Don't forget that Visual Basic strings are 1-based, not 0-based like C++ strings: <P><PRE><FONT FACE="COURIER" SIZE="2">sChar = sTmp.Mid(7, 6); // sChar=="Native"
sChar = sTmp.Mid(7); // sChar=="NativeSlowFastCD"
sChar = sTmp.Left(6); // sChar=="Narrow"
sChar = sTmp.Right(6); // sChar=="FastCD"
</FONT></PRE><P>An additional challenge (left as an exercise for the reader) is to add the Visual Basic <b>Mid</b> statement to insert characters into a string. <P><h4>String Transformations</H4><P>The String class has some transformation functions in both method and function versions: <P><PRE><FONT FACE="COURIER" SIZE="2">// sWide=="Fine"
sWide.UCase(); // sWide=="FINE"
sWide.LCase(); // sWide=="fine"
sWide.Reverse(); // sWide=="enif"
sChar = UCase(sWide); // sChar=="ENIF", sWide=="enif"
sChar = LCase(sWide); // sChar=="enif", sWide=="enif"
sChar = Reverse(sWide); // sChar=="fine", sWide=="enif"
</FONT></PRE><P>There are also similar versions of the <b>Trim</b>, <b>LTrim</b>, and <b>RTrim</b> functions:<P><PRE><FONT FACE="COURIER" SIZE="2">sChar = Trim(sTmp); // sChar=="Stuff", sTmp==" Stuff "
sTmp.Trim(); // sTmp=="Stuff"
</FONT></PRE><P><h4>String Searching</H4><P>I always found Basic's <b>InStr</b> function confusing, so I called the equivalent String function <b>Find</b>. It can find characters or strings, searching forward or backward, with or without case sensitivity.<P><PRE><FONT FACE="COURIER" SIZE="2">// sTmp="A string in a String in a String in a string"
// "12345678901234567890123456789012345678901234567890"
i = sTmp.Find('S'); // Found at position: 15
i = sTmp.Find('S', ffReverse); // Found at position: 27
i = sTmp.Find('S', ffIgnoreCase); // Found at position: 3
i = sTmp.Find('S', ffReverse | ffIgnoreCase); // Found at position: 39
i = sTmp.Find('Z'); // Found at position: 0
i = sTmp.Find("String"); // Found at position: 15
i = sTmp.Find("String", ffReverse); // Found at position: 27
i = sTmp.Find("String", ffIgnoreCase); // Found at position: 3
i = sTmp.Find("String", ffIgnoreCase | ffReverse); // Found at position: 39
i = sTmp.Find("Ztring"); // Found at position: 0
</FONT></PRE><P>This method is 1-based, so C++ programmers may need to make a mental adjustment when using it. <P>It's not too difficult to think of enhancements for the String type. Just look through the Basic and C++ run-time functions and add anything that looks interesting. It's easy to map existing C++ functions to a natural String format, and it's not much harder to write your own functions that provide string features that C++ lacks. But before you spend a lot of time on this, consider how String is used. In most DLLs, you'll be using the constructors, the conversion operators, and maybe a few logical or assignment operators. Basic already provides its own string functionality, so unless you want to replace it with your own more powerful string library, there's not much point in having a full-featured String type. On the other hand, maybe Basic does need a more powerful string library. Be my guest.<P><h3>How the String Class Works</h3><P>We've talked a lot about how to use the String type, but not much about how it is implemented. This article is not about how to write class libraries in C++, so I haven't explained the internals. However, you'll probably feel a little more comfortable using the class (and it will certainly be easier to enhance it) if you have some idea how String works, so let's take a look under the hood. <P><PRE><FONT FACE="COURIER" SIZE="2">class String
{
friend class Buffer;
public:
// Constructors
String();
String(const String& s);
// Danger! C++ can't tell the difference between BSTR and LPWSTR. If
// you pass LPWSTR to this constructor, you'll get very bad results,
// so don't. Instead, cast to constant before assigning.
String(BSTR bs);
// Any non-const LPSTR or LPWSTR should be cast to LPCSTR or LPCWSTR
// so that it comes through here.
String(LPCSTR sz);
String(LPCWSTR wsz);
// Filled with given character (default -1 means unitialized allocate).
String(int cch, WCHAR wch = WCHAR(-1));
// Destructor
~String();
.
.
.
private:
BSTR m_bs; // The
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -