| Programmer's Guide to the WvStreams Libraries | ||
|---|---|---|
| Prev | Chapter 2. WvString - dynamic character strings | Next |
WvString WvStringTable::join(const char *joinchars) const
returns a string that join all the elements in WvStringTable by the specified delimiter. If no delimiter is specified, it uses " \t" by default.
void WvStringTable::split(WvStringParm s, const char *splitchars,int limit)
adds an element to the table.
Another way to do this is by making use of the fact that WvStringTable is really a WvHashTable, so we can do add() and append(). The difference is that add and append takes a reference instead of the constant string.
Refer to WvHashTable for more details.
Let's look at an example.
#include "wvstringtable.h"
#include "wvhashtable.h"
#include <stdio.h>
int main()
{
WvStringTable t(10);
// size: 10 elements
// WvStringTable is essentially a WvHashTable
WvString s("one"), s2("two"), s3("three");
t.add(&s, false);
t.add(&s2,false);
t.add(&s3,false);
// t.add("foo") is not allowed
// refer to WvHashTable for more information
printf("%s\n", t.join(",").cstr());
//prints out: one, two, three
printf("%s\n", t.join().cstr());
// By default, t.join() is using " \t" as a delimiter
// prints out: one two three
t.zap();
//erasing all contents of t
t.split("a : b : c : d ", ":");
printf("%s\n", t.join(",").cstr());
// prints out: a , b , c , d
t.split("x");
t.split("y");
t.split("z");
printf("%s\n", t.join(",").cstr());
// prints out: a , b , c , d , x, y, z
return 0;
}