PolyUtil.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * PolyUtil.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 9/23/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyUtil.h"
  10. using namespace Polycode;
  11. vector<std::string> StringUtil::split(const std::string &str, const std::string &delims) {
  12. string::size_type lastPos = str.find_first_not_of(delims, 0);
  13. string::size_type pos = str.find_first_of(delims, lastPos);
  14. vector<string> tokens;
  15. while (string::npos != pos || string::npos != lastPos) {
  16. tokens.push_back(str.substr(lastPos, pos - lastPos));
  17. lastPos = str.find_first_not_of(delims, pos);
  18. pos = str.find_first_of(delims, lastPos);
  19. }
  20. return tokens;
  21. }
  22. string StringUtil::replace(const std::string &str, const std::string &what, const std::string &withWhat) {
  23. vector<std::string> arr = StringUtil::split(str, what);
  24. string retString = "";
  25. for(int i= 0; i < arr.size(); i++) {
  26. retString += arr[i];
  27. if(i < arr.size()-1)
  28. retString += withWhat;
  29. }
  30. return retString;
  31. }
  32. string StringUtil::toLowerCase(const std::string &str) {
  33. string retString = str;
  34. std::transform(retString.begin(), retString.end(), retString.begin(),std::tolower);
  35. return retString;
  36. }
  37. string StringUtil::floatToString(float value) {
  38. char temp[128];
  39. sprintf(temp, "%f", value);
  40. return string(temp);
  41. }