项目作者: p-ranav

项目描述 :
String Manipulation API for C++
高级语言: C++
项目地址: git://github.com/p-ranav/strcpp.old.git
创建时间: 2017-07-22T14:48:42Z
项目社区:https://github.com/p-ranav/strcpp.old

开源协议:MIT License

下载


String Manipulation API for C++

strcpp::split

  1. {
  2. std::string input = "We are the champions"; // input string
  3. std::vector<std::string> result; // result vector
  4. result = strcpp::split(input, " "); // result = ["We", "are", "the", "champions"]
  5. result = strcpp::split(input, " a"); // result = ["We", "re the champions"]
  6. result = strcpp::split(input, "ions"); // result = ["We are the champ"]
  7. input = "a, b, c, d, e";
  8. result = strcpp::split(input, ", "); // result = ["a", "b", "c", "d", "e"]
  9. input = "LOGGER::ERROR::Error Message!";
  10. auto message = strcpp::split(input, "::")[2]; // message = "Error Message!"
  11. }

strcpp::join

  1. {
  2. std::string input = "This. is. not. how. sentences. work."; // wow such a sentence
  3. auto split_string = strcpp::split(input, ". "); // split using period
  4. std::string joined_string = strcpp::join(split_string, " "); // join result of split
  5. std::string output = strcpp::replace(joined_string, " not", ""); // remove the word 'not'
  6. std::cout << output << std::endl; // "This is how sentences work."
  7. input = "Transaction_date, Product, Price, State"; // some csv data
  8. split_string = strcpp::split(input, ", "); // split using comma
  9. output = strcpp::join(split_string, "::"); // join result of split
  10. std::cout << output << std::endl; // "Transaction_date::Product::Price::State"
  11. }

strcpp::slice

  1. {
  2. std::string input = "Hello World";
  3. // End index is optional and defaults to end of string
  4. std::cout << strcpp::slice(input, 0) << std::endl; // All but the first zero characters - "Hello World"
  5. std::cout << strcpp::slice(input, 0, 1) << std::endl; // Just the first character - "H"
  6. std::cout << strcpp::slice(input, 3) << std::endl; // All but the first three characters - "lo World"
  7. std::cout << strcpp::slice(input, 0, 5) << std::endl; // Just the first five characters - "Hello"
  8. std::cout << strcpp::slice(input, 3, 8) << std::endl; // After third till eigth character - "lo Wo"
  9. std::cout << strcpp::slice(input, -3) << std::endl; // Just the last three characters - "rld"
  10. std::cout << strcpp::slice(input, 0, -3) << std::endl; // All but the last three characters - "Hello Wo"
  11. }

strcpp::contains

  1. {
  2. std::string input = "ERROR::Houston, we have a problem!";
  3. if (strcpp::contains(input, "ERROR")) // containment check - case sensitive
  4. std::cout << "Input string contains \"ERROR\"" << std::endl; // check returns true and prints message
  5. if (strcpp::contains(input, "error::", true)) // containment check - ignore case
  6. std::cout << "Input string contains \"error::\"" << std::endl; // check returns true and prints message
  7. if (strcpp::contains(input, "PROBLEM")) // containment check - ignore case
  8. std::cout << "Input string contains \"PROBLEM\"\n";
  9. else
  10. std::cout << "Input string does not contain \"PROBLEM\"\n"; // check returns false and prints message
  11. }

strcpp::count

  1. {
  2. std::string input = "Hello World!";
  3. std::cout << strcpp::count(input, "l") << std::endl; // prints 3
  4. std::cout << strcpp::count(input, "llo") << std::endl; // prints 1
  5. std::cout << strcpp::count(input, " W") << std::endl; // prints 1
  6. std::cout << strcpp::count(input, " wo") << std::endl; // prints 0
  7. std::cout << strcpp::count(input, " wo", true) << std::endl; // prints 1 - ignores case
  8. std::string them_spaces = "Count, the number of spaces";
  9. std::cout << strcpp::count(them_spaces, " ") << std::endl; // prints 13
  10. }

strcpp::starts_with

  1. {
  2. std::string input = "What's up with the Quaithe?";
  3. if (strcpp::starts_with(input, 'W'))
  4. std::cout << "Input string starts with 'W'\n"; // check returns true and prints message
  5. if (strcpp::starts_with(input, 'w', true))
  6. std::cout << "Input string starts with 'w' - Case is ignored\n"; // check returns true and prints message
  7. }

strcpp::ends_with

  1. {
  2. std::string input = "What's up with the Quaithe?";
  3. if (strcpp::ends_with(input, '.')) // check returns false
  4. std::cout << "Input string ends with period\n";
  5. if (strcpp::ends_with(input, '?')) // check returns true and prints message
  6. std::cout << "Input string is a question\n";
  7. }

strcpp::repeat

  1. {
  2. std::string input = "Ha";
  3. std::cout << strcpp::repeat(input, 5) << std::endl; // "HaHaHaHaHa"
  4. std::cout << strcpp::repeat(input, 5, " ") << std::endl; // "Ha Ha Ha Ha Ha"
  5. input = "Ch";
  6. auto result = strcpp::repeat(
  7. strcpp::repeat(input, 2, "ooo!"),
  8. 3, " ");
  9. std::cout << result << std::endl; // "Chooo!Chooo! Chooo!Chooo! Chooo!Chooo!"
  10. }

strcpp::find

  1. {
  2. std::string input = "I scream, you scream, we all scream for icecream!";
  3. size_t find_index = strcpp::find(input, "you"); // Index = 10
  4. std::cout << find_index << std::endl;
  5. }

strcpp::find_first

  1. {
  2. std::string input = "I scream, you scream, we all scream for icecream!";
  3. size_t find_index = strcpp::find_first(input, "cream"); // Index = 3
  4. std::cout << find_index << std::endl;
  5. }

strcpp::find_last

  1. {
  2. std::string input = "I scream, you scream, we all scream for icecream!";
  3. size_t find_index = strcpp::find_last(input, "cream"); // Index = 47
  4. std::cout << find_index << std::endl;
  5. }

strcpp::find_regex

  1. {
  2. std::string log(R"(
  3. Speed : 366
  4. Mass : 35
  5. Speed : 378
  6. Mass : 32
  7. Speed : 400
  8. Mass : 30)");
  9. std::string regex_string = R"(Speed : \d*)";
  10. auto results = strcpp::find_regex(log, regex_string);
  11. // ["Speed : 366", "Speed : 378", "Speed : 400"]
  12. std::string file_path = "/home/pranav/dev/FILE_SomeStringOfInterest_EVENT.bash";
  13. results = strcpp::find_regex(file_path, ".*FILE_(\\w+)_EVENT\\.bash.*");
  14. // results: ["/home/pranav/dev/FILE_SomeStringOfInterest_EVENT.bash", "SomeStringOfInterest"]
  15. }

strcpp::replace

  1. {
  2. std::string input = "This is a test string.";
  3. std::cout << strcpp::replace(input, "a test", "an example"); // "This is an example string."
  4. std::cout << std::endl <<
  5. strcpp::replace(input, "test", "") << std::endl; // "This is a string"
  6. input = "This is a a test string"; // replace the first occurrence of the letter 'a'
  7. std::cout << strcpp::replace(input, "a", "", 1) << "\n"; // "This is a test string"
  8. input = "Peter Piper picked a peck of pickled peppers.";
  9. std::cout << strcpp::replace(input, "p", "T", 2) << "\n"; // "Peter PiTer Ticked a peck of pickled peppers."
  10. input = "Peter Piper picked a peck of pickled peppers.";
  11. std::cout << strcpp::replace(input, "P", "T", 2) << "\n"; // "Teter Tiper picked a peck of pickled peppers."
  12. }

strcpp::translate

  1. {
  2. std::string input = "1X444 2X3AB *^$RB (F(QP."; // coded input message
  3. std::map<std::string, std::string> translation_table = {
  4. { "1X444", "Appetite"},
  5. { "2X3AB", "comes" },
  6. { "*^$RB", "with" },
  7. { "(F(QP", "eating" }
  8. };
  9. std::string translated = strcpp::translate(input, translation_table);
  10. std::cout << translated << std::endl; // "Appetite comes with eating."
  11. }

strcpp::trim

  1. {
  2. std::string input = " Hello World! ";
  3. std::cout << strcpp::trim(input) << std::endl; // "Hello World!"
  4. std::cout << strcpp::ltrim(input) << std::endl; // "Hello World! "
  5. std::cout << strcpp::rtrim(input) << std::endl; // " Hello World!"
  6. }

strcpp::format

  1. {
  2. std::string output = strcpp::format("Roses are {0}, Violets are {1}, Sugar is {2}, And so are {3}!",
  3. { "red", "blue", "sweet", "you" });
  4. std::cout << output << std::endl; // "Roses are red, Violets are blue, Sugar is sweet, And so are you!"
  5. std::cout << strcpp::format("{0}!... {0}!", {"Hodor"}) << std::endl; // "Hodor!... Hodor!"
  6. }

strcpp::upper

  1. {
  2. std::string input = "this is some lowercase string";
  3. std::cout << strcpp::upper(input) << std::endl; // "THIS IS SOME LOWERCASE STRING"
  4. }

strcpp::lower

  1. {
  2. std::string input = "ACCIDENTALLY ENABLED CAPS LOCK!!!";
  3. std::cout << strcpp::lower(input) << std::endl; // "accidentally enabled caps lock!!!"
  4. }

LICENSE

Copyright (c) 2017 Pranav Srinivas Kumar pranav.srinivas.kumar@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.