2024-09-21 21:13:34 -06:00
|
|
|
#include "string_functions.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2024-09-24 16:56:57 -06:00
|
|
|
unsigned long string_length(const string STR) {
|
|
|
|
unsigned long result = -1;
|
|
|
|
result = STR.length(); // set result to the length of the string
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
char string_char_at(const string STR, const int IDX) {
|
2024-09-24 16:56:57 -06:00
|
|
|
char result = '\0';
|
|
|
|
result = STR.at(IDX);
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:56:57 -06:00
|
|
|
string string_append(const string LEFT, const string RIGHT) {
|
|
|
|
string result = LEFT;
|
|
|
|
result = LEFT + RIGHT;
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
string string_insert(const string STR, const string TO_INSERT, const int IDX) {
|
2024-09-24 16:56:57 -06:00
|
|
|
string result = STR;
|
|
|
|
result.insert(IDX, TO_INSERT);
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:56:57 -06:00
|
|
|
size_t string_find(const string STR, const char C) {
|
|
|
|
size_t result = 0;
|
|
|
|
result = STR.find_first_of(C);
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
string string_substring(const string STR, const int IDX, const int LEN) {
|
2024-09-24 16:56:57 -06:00
|
|
|
string result = STR;
|
|
|
|
result = STR.substr(IDX, LEN);
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:56:57 -06:00
|
|
|
string string_replace(const string STR, const string TEXT_TO_REPLACE,
|
|
|
|
const string REPLACE_WITH) {
|
|
|
|
string result = STR;
|
|
|
|
auto index = STR.find(TEXT_TO_REPLACE);
|
|
|
|
if (index != -1ul) {
|
|
|
|
result.replace(STR.find(TEXT_TO_REPLACE), TEXT_TO_REPLACE.length(),
|
|
|
|
REPLACE_WITH);
|
|
|
|
}
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:56:57 -06:00
|
|
|
string string_first_word(const string STR) {
|
|
|
|
string result = STR;
|
|
|
|
result = STR.substr(0, STR.find_first_of(' '));
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:56:57 -06:00
|
|
|
string string_remove_first_word(const string STR) {
|
|
|
|
string result = STR;
|
|
|
|
if (STR.find_first_of(' ') != -1ul) {
|
|
|
|
result = STR.substr(STR.find_first_of(' ') + 1, STR.length() - 1);
|
|
|
|
} else {
|
|
|
|
result = "";
|
|
|
|
}
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:56:57 -06:00
|
|
|
string string_second_word(const string STR) {
|
|
|
|
string result = STR;
|
|
|
|
result = string_remove_first_word(STR);
|
|
|
|
result = string_first_word(result);
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:56:57 -06:00
|
|
|
string string_third_word(const string STR) {
|
|
|
|
string result = STR;
|
|
|
|
result = string_remove_first_word(STR);
|
|
|
|
result = string_remove_first_word(result);
|
|
|
|
result = string_first_word(result);
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:56:57 -06:00
|
|
|
string string_nth_word(const string STR, const int N) {
|
|
|
|
string result = STR;
|
|
|
|
for (int i = 0; i < N - 1; i++) {
|
|
|
|
result = string_remove_first_word(result);
|
|
|
|
}
|
|
|
|
result = string_first_word(result);
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> string_tokenize(const string STR, const char DELIMINATOR) {
|
2024-09-24 16:56:57 -06:00
|
|
|
vector<string> result;
|
|
|
|
result = {""};
|
|
|
|
for (unsigned long i = 0; i < STR.length(); i++) {
|
|
|
|
if (STR[i] == DELIMINATOR) {
|
|
|
|
result.push_back("");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result[result.size() - 1] += STR[i];
|
|
|
|
}
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
2024-09-24 16:56:57 -06:00
|
|
|
string string_substitute(const string STR, const char TARGET,
|
|
|
|
const char REPLACEMENT) {
|
|
|
|
string result = STR;
|
|
|
|
while (result.find(TARGET) != -1ul) {
|
|
|
|
result[result.find(TARGET)] = REPLACEMENT;
|
|
|
|
}
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
string string_to_lower(const string STR) {
|
2024-09-24 16:56:57 -06:00
|
|
|
string result = STR;
|
|
|
|
for (unsigned int i = 0; i < STR.length(); i++) {
|
|
|
|
char newChar = STR[i];
|
|
|
|
if (newChar >= 'A' && newChar <= 'Z') {
|
|
|
|
newChar = (char)(newChar + 32);
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
2024-09-24 16:56:57 -06:00
|
|
|
result[i] = newChar;
|
|
|
|
}
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
string string_to_upper(const string STR) {
|
2024-09-24 16:56:57 -06:00
|
|
|
string result = STR;
|
|
|
|
for (unsigned int i = 0; i < STR.length(); i++) {
|
|
|
|
char newChar = STR[i];
|
|
|
|
if (newChar >= 'a' && newChar <= 'z') {
|
|
|
|
newChar = (char)(newChar - 32);
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
2024-09-24 16:56:57 -06:00
|
|
|
result[i] = newChar;
|
|
|
|
}
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int string_compare(const string LHS, const string RHS) {
|
2024-09-24 16:56:57 -06:00
|
|
|
int result = 0;
|
|
|
|
if (LHS > RHS) {
|
|
|
|
result = 1;
|
|
|
|
} else {
|
|
|
|
if (LHS < RHS) {
|
|
|
|
result = -1;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|
2024-09-24 16:56:57 -06:00
|
|
|
}
|
|
|
|
return result;
|
2024-09-21 21:13:34 -06:00
|
|
|
}
|