Files
VisualSapfor/src/files/Planner/String.h

74 lines
1.9 KiB
C
Raw Normal View History

2023-12-10 16:20:10 +03:00
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <string>
class String {
friend String operator+(const String& a, const String& b);
string body;
public:
String() { body = ""; }
String(const char* s) { body = s; }
String(const char* s, char ps) {
body = s;
for (long i = 0; i < getLength(); ++i)
body[i] = (s[i] == ps) ? '\n' : s[i];
}
String(int s) { body = to_string(s); }
String(long s) { body = to_string(s); }
String(long long s) { body = to_string(s); }
void println() const { printf("[%s]\n", body.c_str()); }
void addChar(char c) { body += c; }
const char* getCharArray() const { return body.c_str(); }
const string& getBody() const { return body; }
size_t getLength() const { return body.size(); }
bool isEmpty() const { return body.size() == 0; }
bool contains(const String& s) const { return body.find(s.getBody()) != string::npos; }
bool operator==(const String& s) const { return body == s.getBody(); }
const String& operator=(const String& s) {
body = s.getBody();
return *this;
}
static String DQuotes(const String& s) {
string tmp = '"' + s.getBody() + '"';
return String(tmp.c_str());
}
String Replace(char f, char t) {
String res(body.c_str());
for (auto i = 0; i < getLength(); ++i)
res.body[i] = (body[i] == f) ? t : body[i];
return res;
}
String Remove(char f) {
String res;
for (auto i = 0; i < getLength(); ++i)
if (body[i] != f)
res.addChar(body[i]);
return res;
}
String toUpper() {
String res = String(this->getCharArray());
for (auto i = 0; i < getLength(); ++i)
res.body[i] = toupper(body[i]);
#if DEB
res.println();
#endif
return res;
}
};
String operator+(const String& a, const String& b) {
return String((a.getBody() + b.getBody()).c_str());
}