1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#include <iostream>
#include "estring.hpp"
using namespace std;
// take a line and split it into a vector
svector str::split(str split, uint limit) {
svector list;
size_t end,pos = 0;
while(true) {
end = find(split,pos);
list.push_back(substr(pos,end-pos));
pos = end + split.size();
if(end == string::npos) break;
if(limit != 0) {
if(list.size() >= limit) {
list.push_back(substr(pos));
break;
}
}
}
return list;
}
// combine a svector and replace string
void str::join(str sept, svector &list) {
assign(list[0]);
for(uint i = 1; i < list.size(); i++) {
append(sept);
append(list[i]);
}
}
// append a svector to the string
void str::ajoin(str sept, svector &list) {
for(uint i = 0; i < list.size(); i++) {
append(sept);
append(list[i]);
}
}
// counts how many times it appears
uint str::count(const str s) const {
uint total = 0;
size_t end,pos = 0;
while(true) {
end = find(s,pos);
if(end == string::npos)
break;
pos = end + s.size();
total++;
}
return total;
}
bool str::islower() const {
bool Bool = true;
if (length() == 0)
Bool = false;
else {
for(const_iterator i = begin(); i != end(); i++) {
if(*i >= 'A' && *i <= 'Z') {
Bool = false;
break;
}
}
}
return Bool;
}
bool str::isupper() const {
bool Bool = true;
if (length() == 0)
Bool = false;
else {
for(const_iterator i = begin(); i != end(); i++) {
if(*i >= 'a' && *i <= 'z') {
Bool = false;
break;
}
}
}
return Bool;
}
bool str::isint() const {
bool Bool = true;
if (length() == 0)
Bool = false;
else {
for(const_iterator i = begin(); i != end(); i++) {
if(*i < '0' || *i > '9') {
Bool = false;
break;
}
}
}
return Bool;
}
bool str::isfloat() const {
bool Bool = true;
if (length() == 0)
Bool = false;
else if( count(".") != 1 )
Bool = false;
else {
for(const_iterator i = begin(); i != end(); i++) {
if((*i < '0' || *i > '9') && *i != '.') {
Bool = false;
break;
}
}
}
return Bool;
}
bool str::isalpha() const {
bool Bool = true;
if (length() == 0)
Bool = false;
else {
for(const_iterator i = begin(); i != end(); i++) {
if((*i < 'a' || *i > 'z') && (*i < 'A' || *i > 'Z') && *i != ' ') {
Bool = false;
break;
}
}
}
return Bool;
}
str str::lower() {
for(iterator i = begin(); i != end(); i++) {
if(*i >= 'A' && *i <= 'Z')
*i += ('a' - 'A');
}
return *this;
}
str str::upper() {
for(iterator i = begin(); i != end(); i++) {
if(*i >= 'a' && *i <= 'z')
*i -= ('a' - 'A');
}
return *this;
}
int main() {
str combine("81.1");
str test("split this line");
svector slist;
float nfloat;
int nint;
// convert into number
nfloat = combine.convert<float>();
cout << nfloat << endl;
// convert into number
nint = combine.convert<int>();
cout << nint << endl;
// count how many times it appears in string
cout << test.count("li") << endl;
cout << test.count("e") << endl;
cout << test.count("I") << endl;
// seeing if its all char
if(test.isalpha())
cout << "True" << endl;
else
cout << "False" << endl;
cout << test << endl;
// split on a str
slist = test.split(" ",1);
for(uint i = 0; i < slist.size(); i++)
cout << slist[i] << endl;
// join it back to a string
combine.join(" ", slist);
cout << combine << endl;
// appending , and join to end of string
combine.ajoin(",",slist);
cout << combine << endl;
// change all letters to uppercase
cout << test.upper() << endl;
// change all letters to lowercase
cout << test.lower() << endl;
return 0;
}
|