增加字符串相关操作

This commit is contained in:
Nomango 2018-05-01 17:19:55 +08:00
parent 904218e74d
commit 6797570015
3 changed files with 77 additions and 29 deletions

View File

@ -83,7 +83,7 @@ e2d::String e2d::String::format(const char * format, ...)
size_t num_of_chars = _vscprintf(format, marker);
if (num_of_chars > tmp.capacity())
if (num_of_chars > tmp.capacity())
{
tmp.resize(num_of_chars + 1);
}
@ -118,6 +118,11 @@ e2d::String e2d::String::format(const wchar_t * format, ...)
return std::move(str);
}
void e2d::String::swap(String & str1, String & str2)
{
str1.m_str.swap(str2.m_str);
}
e2d::String & e2d::String::operator=(const String &str)
{
m_str = str.m_str;
@ -359,6 +364,11 @@ std::string e2d::String::getCString() const
return std::move(str);
}
int e2d::String::compare(const String & str) const
{
return m_str.compare(str.m_str);
}
e2d::String e2d::String::toUpper() const
{
String str(*this);
@ -385,7 +395,7 @@ int e2d::String::toInt() const
{
return 0;
}
return _wtoi(m_str.c_str());
return std::stoi(m_str, 0, 10);
}
double e2d::String::toDouble() const
@ -394,7 +404,7 @@ double e2d::String::toDouble() const
{
return 0.0;
}
return _wtof(m_str.c_str());
return std::stod(m_str, 0);
}
bool e2d::String::toBool() const
@ -413,39 +423,55 @@ bool e2d::String::toBool() const
e2d::String e2d::String::subtract(int offset, int count) const
{
String temp;
String tmp;
int length = getLength();
if (length == 0 || offset >= length)
return std::move(temp);
return std::move(tmp);
offset = offset >= 0 ? offset : 0;
if (count < 0 || (offset + count) > length)
count = length - offset;
temp.m_str = m_str.substr(offset, count);
return std::move(temp);
tmp.m_str = m_str.substr(offset, count);
return std::move(tmp);
}
int e2d::String::findFirstOf(const wchar_t ch) const
void e2d::String::insert(const String & str, int pos)
{
for (int i = 0; i < getLength(); i++)
if (m_str[i] == ch)
return i;
return -1;
m_str.insert(static_cast<size_t>(pos), str.m_str);
}
int e2d::String::findLastOf(const wchar_t ch) const
void e2d::String::replace(const String & from, const String & to)
{
int index = -1;
if (from.m_str.empty())
return;
for (int i = 0; i < getLength(); i++)
if (m_str[i] == ch)
index = i;
size_t start_pos = 0;
while ((start_pos = m_str.find(from, start_pos)) != std::string::npos)
{
m_str.replace(start_pos, from.m_str.length(), to);
start_pos += to.m_str.length();
}
}
return index;
void e2d::String::erase(int offset, int count)
{
m_str.erase(static_cast<size_t>(offset), static_cast<size_t>(count));
}
int e2d::String::find(const String & str, int offset) const
{
size_t index;
if ((index = m_str.find(str.m_str, static_cast<size_t>(offset))) == std::wstring::npos)
{
return -1;
}
else
{
return static_cast<int>(index);
}
}
void e2d::String::clear()

View File

@ -133,7 +133,7 @@ e2d::String e2d::Path::getFileExtension(String filePath)
{
String fileExtension;
// 找到文件名中的最后一个 '.' 的位置
int pos = filePath.findLastOf(L'.');
int pos = filePath.getWString().find_last_of(L'.');
// 判断 pos 是否是个有效位置
if (pos != -1)
{

View File

@ -93,20 +93,39 @@ public:
// »ñÈ¡ ANSI ×Ö·û´®
std::string getCString() const;
// 获取裁剪字符串
String subtract(
// 比较字符串
int compare(
const String & str
) const;
// 截取字符串
e2d::String subtract(
int offset, /* Æ«ÒÆÁ¿ */
int count = -1 /* ½ØÈ¡×Ö·ûÊýÁ¿ */
) const;
// 获取字符串中第一个特定字符的下标
int findFirstOf(
const wchar_t ch
) const;
// 插入字符串
void insert(
const String & str,
int pos
);
// 获取字符串中最后一个特定字符的下标
int findLastOf(
const wchar_t ch
// 替换字符串中的指定内容
void replace(
const String & from, /* 需替换内容 */
const String & to /* 替换成内容 */
);
// 删除字符串中的指定内容
void erase(
int offset, /* 偏移量 */
int count /* 删除字符数量 */
);
// 搜索字符串
int find(
const String & str, /* 查找内容 */
int offset = 0 /* 偏移量 */
) const;
// Çå¿Õ×Ö·û´®
@ -137,6 +156,9 @@ public:
static String format(const char * format, ...);
static String format(const wchar_t * format, ...);
// 交换两字符串
static void swap(String &str1, String &str2);
// ¸³ÖµÔËËã·û
String& operator= (const String &);
String& operator= (const char *);