String

by Joe Strout

This is a general-purpose string class. It was designed with Macintosh programming in mind; its internal data format is a Pascal string, so it can be passed directly (as input OR output parameter!) to Toolbox calls. But it also works just fine under on other systems; I've used it in both DOS and Unix without a hitch.

(I once had a similar string class for VMS, internally formatted as a string descriptor. It worked great; I don't know why the Big Guys don't supply such classes with their compilers.)

You can browse the functions and operators below. And you can grab the code (which is mostly self-explanatory):

Operators
OperatorDescriptionExample Usage
=assignment myString = "wowzers";
myString = myCharArray;
const char*conversion expectsCharArry(myString);
==
!=
>
<
>=
<=
comparison if (myString1 < myString2)
if (myString == "wowzers")
while (myString != "")
[]array-like indexing firstChar = myString[0];
()same as Substr firstThree = myString(0,2)
+concatenation myString = myStr1 + myStr2;
myString = (String)"this" + "that";
+=extension myString += "moreStuff";
myString += myOtherStr;
*repetition threeWows = (String)"wow" * 3;
*=self-repetition bearsRepeating *= 2;
<<stream output cout << myString;
>>stream input cin >> oneWord;

Member Functions
FunctionsDescriptionExample Usage
Length()length of string myWowStr.Length() == 3
Index(s)location of substring pos = myString.Index("sub");
Element(i,c)element extraction secondWord = myString.Element(1);
field3 = myString.Element(2,'\t');
Substr(i,j)substring extraction cde = myAlphabet.Substr(2,4);
Left(i)leftmost chars abcd = myAlphabet.Left(4);
Right(i)rightmost chars xyz = myAlphabet.Right(3);
Trim()remove leading/trailing blanks if (myCmd.Trim() == "run")

Friend Functions
FunctionsDescriptionExample Usage
IntValue(s)convert to int if (IntValue(myString) == 42)
Plural(s)make plural cout << Plural(myString);
Uppercase(s)make all CAPS if (Uppercase(myString) == "WOW")

http://www-ncmir.ucsd.edu/~jstrout/classlib/String.html
Last Updated: 9/23/96 Joe Strout ( joe@strout.net )