增加字符串相关操作

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

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

View File

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

View File

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