A Super String Class | CodeGuru

A Super String Class

Yet another string class? This class got its start a number of years ago. At that time, it was common to have to write even the simplest data structure class. Since then it has gradually grown and mutated at irregular intervals. The result is fairly ambitious, for a string class. Super String features string and […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 27, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Yet another string class?

This class got its start a number of years ago. At that time, it was common to have to write
even the simplest data structure class. Since then it has gradually grown and mutated at
irregular intervals. The result is fairly ambitious, for a string class.

Super String features string and buffer operations, conversions and comparisons, extensible
searching, and C compatibility.

The complete documentation is here.

Here is the header, implementation,
and example code. These are marked-up html files and so are fairly
large.

Examples

Here is some code to demo the various parts of the class:

	{
		// assign a string some values
		SS ss (“hello”);
		// the new value is “65” not “A”
		ss = 65;
		// the new value is “65c”
		ss += ‘c’;
	}
	{
		// allocate a buffer
		SS ss (SS::Buffer(6));
		// indexing, length() and size() are the same
		for (int i=0; i<ss.length(); i++) ss[i] = ‘a’ + i;
		// substring, the current value is “abcdef”
		ss (2,3) == “cde”;
		// the new value is “ab12f”
		ss (2,3) = “12”;
		// the new value is “abxyf”
		ss.set (“xy”, 2);
		// the new value is “abf”, “xy” is returned
		ss.cut (2, 2) == “xy”;
		// now back to “abcdef”
		ss.paste (“cde”, 2);
		// fill with x’s, “xxxxxx”
		ss.fill (‘x’);
	}
	{
		// comparisons
		SS ss(12);
		// 12 is less than 13
		ss < 13;
		ss = “one”;
		// “three” is greater than “one”
		“three” >= ss;
	}
	{
		// searching
		SS ss (“The next one”);
		// find the word “next”
		int p = ss.findNext (“next”);
		// can’t find the word “first”
		p = ss.findNext (“first”); // p == SS::notfound
		// find the first whitespace character
		p = ss.findNext (SS::whitespace);
		// find the next whitespace character
		p = ss.findNext (SS::whitespace, p + 1);
		// find the word “one”
		SS result;
		ss.findNext (SS::FindOneOrMore (SS::blackspace), p, result);
		result == “one”;
		// ss contains “The”
		bool b = ss.contains (“The”); // b == true
		// there are 9 lowercase characters
		int n = ss.population (SS::lowercase); // n == 9
		// remove whitespace, ss is now “Thenextone”
		ss.remove (SS::whitespace);
		// replace the ‘T’, ss is now “thenextone”
		ss.replace (SS::uppercase, “t”);
		std::cout << “the string is “” << ss << “”” << std::endl;
	}
	{
		// numeric
		SS ss (“100”);
		// extract the number 100
		long v = ss.toLong();
		// create a base 16 string
		SS::toBase (v, 16) == “64”;
	}
	{
		// matching
		SS ss (“theOne1 theTwo2 theThree3”);
		// an empty vector for results
		// each element will be a position and a length
		SS::BegLenVect result;
		// get positions of all the “the”s
		ss.match (“the”, result);
		// reverse the “the”s
		ss.reverse (result) == “ehtOne1 ehtTwo2 ehtThree3”;
		// remove the now reversed “the”s
		ss.removeRange (result) == “One1 Two2 Three3”;
		// break up into tokens, reusing result
		ss.tokenize (result);
		// change string to “One1 Two2 Four4”
		ss (result[2]) = “Four4”;
		// get a vector with the digits
		ss.match (SS::digit, result);
		// cut the ‘4’
		ss.cut (result[2]);
		// remove the ‘2’ by limiting the remove range
		ss.remove (SS::digit, result[1]);
		// remove the 1 via an ‘X’
		ss.fill (‘X’, result[0]).remove (‘X’) == “One Two Four”;
		std::cout << “the string is “” << ss << “”” << std::endl;
	}
Advertisement

CString compatibility

Support for additional types can be added externally to the class. A header file,
“QString.h”, is provided to supply compatibility for CString. To allow a CString to
convert to a Super String:

typedef SS QString;
inline void SSconvert (CString const & u, QString & w)
{
	w.assign ((char const *)u, u.GetLength());
}

A Super String can convert to a CString via operator char*. To allow relational
comparisons, e.g. ==, >, etc. define:

inline int SScompare (QString const & u, CString const & w)
{
	return u.compare((char const *)w,w.GetLength());
}

Portability

The source and test code have been compiled with VC6.0, sp2. The only things Microsoft-specific
should be the numeric type __int64, often referred to as long long, and _vsnprintf, a variant of
sprintf.

To do

Possible performance enhancements, UNICODE support.

Download source + doc + demo proj + test code – 62 KB

Download source only – 24 KB

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.