initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / test / string / stringTest.C
blob2ed59bf88f7b4ab293a37bfe71b8251d51e38aea
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
27 \*---------------------------------------------------------------------------*/
29 #include "string.H"
30 #include "IOstreams.H"
32 using namespace Foam;
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 // Main program:
37 int main(int argc, char *argv[])
39     string test("$HOME kjhkjhkjh \" \\$HOME/tyetyery ${FOAM_RUN} \n ; hkjh ;$");
41     Info<< "string:" << test << nl << "hash:"
42         << unsigned(string::hash()(test)) << endl;
44     // test sub-strings via iterators
45     string::const_iterator iter  = test.end();
46     string::const_iterator iter2 = test.end();
47     string::size_type fnd = test.find('\\');
49     if (fnd != string::npos)
50     {
51         iter  = test.begin() + fnd;
52         iter2 = iter + 6;
53     }
55     Info<< "sub-string via iterators : >";
56     while (iter != iter2)
57     {
58         Info<< *iter;
59         iter++;
60     }
61     Info<< "<\n";
63     Info<< string(test).replace("kj", "zzz") << endl;
64     Info<< string(test).replace("kj", "") << endl;
65     Info<< string(test).replaceAll("kj", "zzz") << endl;
66     Info<< string(test).replaceAll("kj", "z") << endl;
68     Info<< string(test).expand() << endl;
70     string test2("~OpenFOAM/controlDict");
71     Info<< test2 << " => " << test2.expand() << endl;
73     // replace controlDict with "newName"
74     {
75         string::size_type i = test2.rfind('/');
77         if (i == string::npos)
78         {
79             test2 = "newName";
80         }
81         else
82         {
83             // this uses the std::string::replace
84             test2.replace(i+1, string::npos, "newName");
85         }
86         Info<< "after replace: " << test2 << endl;
88         // do another replace
89         // this uses the Foam::string::replace
90         test2.replace("OpenFOAM", "openfoam");
92         Info<< "after replace: " << test2 << endl;
93     }
95     string s;
96     Sin.getLine(s);
98     string s2(s.expand());
100     cout<< "output string with " << s2.length() << " characters\n";
101     cout<< "ostream<<  >" << s2 << "<\n";
102     Info<< "Ostream<<  >" << s2 << "<\n";
103     Info<< "hash:" << hex << string::hash()(s2) << endl;
105     Info << "End\n" << endl;
107     return 0;
111 // ************************************************************************* //