ENH: add string replaceAny() method

- takes a search string and a replacement character.
  The replacement character can also be a nul char ('\0'), which
  simply removes the characters.

  Possible uses:

  * Replace reserved characters
      str.replaceAny("<>:", '_');

  * Remove shell meta-characters or reserved filesystem characters
      str.replaceAny("*?<>{}[]:", '\0');
This commit is contained in:
Mark Olesen
2020-04-28 14:11:22 +02:00
parent 344940829a
commit 14e2dbfb2a
3 changed files with 53 additions and 3 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2013 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -173,6 +173,19 @@ int main(int argc, char *argv[])
Info<<"trimRight: " << stringOps::trimRight(test) << endl;
Info<<"trim: " << stringOps::trim(test) << endl;
// With replace, replaceAll
{
string test2(test);
Info<<"replaceAny: (\"abcj\", '?')" << nl
<< test2.replaceAny("abcj", '?') << endl;
Info<<"replaceAll: (\"k\", \"?\")" << nl
<< test2.replaceAll("k", "?") << endl;
Info<<"replaceAll: (\"h\", null) - ie, remove them" << nl
<< test2.replaceAll("h", "") << endl;
Info<<"replaceAny: (\"it${}?\", null) - ie, remove them" << nl
<< test2.replaceAny("it${}?", '\0') << endl;
}
if (false)
{
Info<<"test move construct - string size:" << test.size() << nl;