stringutils.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libfbasics - library of some basic tools
  4. * See file License for license information.
  5. */
  6. #include "core/basics/stringutils.h"
  7. #include <core/basics/types.h>
  8. namespace NICE {
  9. std::string itostr(int integer)
  10. {
  11. std::string s;
  12. if(integer<0) {
  13. integer=-integer;
  14. s="-";
  15. }
  16. int k=integer;
  17. int div=1;
  18. while(k>9) {
  19. k/=10;
  20. div*=10;
  21. }
  22. while(div>0) {
  23. s += ((integer/div)%10+48);
  24. div/=10;
  25. }
  26. return s;
  27. }
  28. std::vector<std::string> splitString(const std::string &s, char separator) {
  29. std::vector<std::string> result;
  30. splitString(s, separator, result);
  31. return result;
  32. }
  33. void splitString(const std::string &s, char separator,
  34. std::vector<std::string>& result)
  35. {
  36. int lastpos=0;
  37. int pos=0;
  38. while(pos!=-1) {
  39. pos = s.find_first_of(separator,lastpos);
  40. result.push_back(s.substr(lastpos,pos-lastpos));
  41. lastpos=pos+1;
  42. }
  43. }
  44. std::vector<std::vector<std::string> > splitStringVector(
  45. const std::vector<std::string> &inlist,
  46. char separator)
  47. {
  48. std::vector<std::vector<std::string> > outlist(inlist.size());
  49. for(uint i=0;i<inlist.size();i++) {
  50. splitString(inlist[i], separator, outlist[i]);
  51. // int lastpos=0;
  52. // int pos=0;
  53. // while(pos!=-1) {
  54. // pos = inlist[i].find_first_of(separator,lastpos);
  55. // outlist[i].push_back(inlist[i].substr(lastpos,pos-lastpos));
  56. // lastpos=pos+1;
  57. // }
  58. }
  59. return outlist;
  60. }
  61. std::string replaceChar(const std::string& s, char oldChar, char newChar) {
  62. std::string result(s);
  63. for (uint i = 0; i < s.size(); ++i) {
  64. if (result[i] == oldChar) {
  65. result[i] = newChar;
  66. }
  67. }
  68. return result;
  69. }
  70. } // namespace