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

71 lines
1.8 KiB
C
Raw Normal View History

2023-12-03 19:43:41 +03:00
#pragma once
2023-09-17 22:13:42 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2023-12-03 19:43:41 +03:00
#include <ctype.h>
2023-12-05 14:08:31 +03:00
#include <string>
2023-12-03 19:43:41 +03:00
class String {
friend String operator+(const String& a, const String& b);
2023-12-05 14:08:31 +03:00
string body;
2023-12-03 19:43:41 +03:00
public:
2023-12-05 14:08:31 +03:00
String() { body = ""; }
String(const char* s) { body = s; }
2023-12-03 19:43:41 +03:00
String(const char* s, char ps) {
2023-12-05 14:08:31 +03:00
body = s;
for (long i = 0; i < getLength(); ++i)
2023-12-03 19:43:41 +03:00
body[i] = (s[i] == ps) ? '\n' : s[i];
}
2023-12-05 14:08:31 +03:00
String(int s) { body = to_string(s); }
String(long s) { body = to_string(s); }
String(long long s) { body = to_string(s); }
2023-12-03 19:43:41 +03:00
2023-12-05 14:08:31 +03:00
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(); }
2023-12-03 19:43:41 +03:00
const String& operator=(const String& s) {
2023-12-05 14:08:31 +03:00
body = s.getBody();
2023-12-03 19:43:41 +03:00
return *this;
}
static String DQuotes(const String& s) {
2023-12-05 14:08:31 +03:00
string tmp = '"' + s.getBody() + '"';
return String(tmp.c_str());
2023-12-03 19:43:41 +03:00
}
String Replace(char f, char t) {
2023-12-05 14:08:31 +03:00
String res(body.c_str());
for (auto i = 0; i < getLength(); ++i)
2023-12-03 19:43:41 +03:00
res.body[i] = (body[i] == f) ? t : body[i];
return res;
}
String Remove(char f) {
String res;
2023-12-05 14:08:31 +03:00
for (auto i = 0; i < getLength(); ++i)
2023-12-03 19:43:41 +03:00
if (body[i] != f)
2023-09-17 22:13:42 +03:00
res.addChar(body[i]);
2023-12-03 19:43:41 +03:00
return res;
}
2023-12-05 14:08:31 +03:00
2023-12-03 19:43:41 +03:00
String toUpper() {
2023-09-17 22:13:42 +03:00
String res = String(this->getCharArray());
2023-12-05 14:08:31 +03:00
for (auto i = 0; i < getLength(); ++i)
2023-12-03 19:43:41 +03:00
res.body[i] = toupper(body[i]);
2023-09-17 22:13:42 +03:00
res.println();
return res;
}
2023-12-03 19:43:41 +03:00
};
2023-09-17 22:13:42 +03:00
2023-12-03 19:43:41 +03:00
String operator+(const String& a, const String& b) {
2023-12-05 14:08:31 +03:00
return String((a.getBody() + b.getBody()).c_str());
2023-09-17 22:13:42 +03:00
}