# GFString **Repository Path**: QingDaoGongFa/GFString ## Basic Information - **Project Name**: GFString - **Description**: GFString是一个轻量级但功能强大的字符串处理库,专为嵌入式系统和单片机设计。它提供了全面的字符串操作功能,同时保持内存效率和安全性。 - **Primary Language**: C++ - **License**: Not specified - **Default Branch**: master - **Homepage**: http://www.zyxmcu.com - **GVP Project**: No ## Statistics - **Stars**: 7 - **Forks**: 3 - **Created**: 2024-12-26 - **Last Updated**: 2025-10-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # GFString 字符串处理库 ## 概述 GFString是一个轻量级但功能强大的字符串处理库,专为嵌入式系统和单片机设计。它提供了全面的字符串操作功能,同时保持内存效率和安全性。 ## 主要特性 - 内存高效的字符串操作 - 安全的字符串处理,具有缓冲区溢出保护 - 支持各种数值类型转换 - 丰富的字符串实用函数 - 无动态内存分配(使用固定缓冲区大小) ## 安装方法 1. 将`GFString.h`和`GFString.cpp`复制到您的项目目录 2. 在代码中包含头文件: ```cpp #include "GFString.h" ``` ## 基本用法 ### 创建字符串 ```cpp // 从字面量创建 String str1("Hello"); String str2 = "World"; // 从数字创建 String str3(123); // 从整数创建 String str4(3.14f, 2); // 从浮点数创建,保留2位小数 String str5(123UL, 16, 4); // 从无符号长整型创建,16进制,长度为4("007B") ``` ### 字符串操作 ```cpp String str1 = "Hello"; String str2 = "World"; // 字符串连接 str1.concat(" "); str1.concat(str2); // "Hello World" // 字符串比较 if (str1.equals(str2)) { } if (str1.equalsIgnoreCase(str2)) { } // 字符串搜索 int index = str1.indexOf("World"); bool contains = str1.contains("Hello"); // 字符串修改 str1.toLowerCase(); // 转换为小写 str1.toUpperCase(); // 转换为大写 str1.trim(); // 删除首尾空格 ``` ## API参考 ### 构造函数 - `String()` - 创建空字符串 - `String(const char* cstr)` - 从C字符串创建 - `String(char c)` - 从单个字符创建 - `String(unsigned char value, unsigned char base=10)` - 从无符号字符创建 - `String(int value, unsigned char base=10)` - 从整数创建 - `String(unsigned int value, unsigned char base=10)` - 从无符号整数创建 - `String(long value, unsigned char base=10)` - 从长整型创建 - `String(unsigned long value, unsigned char base=10)` - 从无符号长整型创建 - `String(float value, unsigned char decimalPlaces=2)` - 从浮点数创建 - `String(double value, unsigned char decimalPlaces=2)` - 从双精度浮点数创建 ### 字符串信息 - `length()` - 获取字符串长度 - `capacity()` - 获取缓冲区容量 - `isEmpty()` - 检查字符串是否为空 ### 字符串修改 - `concat()` - 追加字符串/字符/数字 - `remove(unsigned int index)` - 从指定位置删除到末尾 - `remove(unsigned int index, unsigned int count)` - 删除指定数量的字符 - `replace(char find, char replace)` - 替换单个字符 - `replace(const String& find, const String& replace)` - 替换子字符串 - `trim()` - 删除首尾空格 - `toLowerCase()` - 转换为小写 - `toUpperCase()` - 转换为大写 ### 字符串比较 - `equals()` - 比较字符串(区分大小写) - `equalsIgnoreCase()` - 比较字符串(不区分大小写) - `compareTo()` - 比较字符串并返回差异 - `startsWith()` - 检查字符串是否以指定子串开始 - `endsWith()` - 检查字符串是否以指定子串结束 ### 搜索和子串 - `indexOf()` - 查找字符/子串的位置 - `lastIndexOf()` - 查找字符/子串的最后位置 - `substring()` - 提取字符串的一部分 - `charAt()` - 获取指定位置的字符 ### 类型转换 - `toInt()` - 转换为整数 - `toFloat()` - 转换为浮点数 - `toCharArray()` - 转换为字符数组 ### 实用函数 - `reserve()` - 检查缓冲区是否能容纳指定长度 - `changeBuffer()` - 更改缓冲区大小 - `isNum()` - 检查字符串是否为有效数字 - `GetNum()` - 从字符串中提取数字 ## 内存管理 该库使用固定大小的缓冲区(ZYXMCU_STRING_BUFF_LEN)来存储字符串。这可以防止堆内存碎片化,使内存使用可预测,这对嵌入式系统来说非常重要。 ## 错误处理 - 缓冲区溢出保护 - 安全的字符串操作 - 无效数字处理 - 空指针检查 ## 性能考虑 - 无动态内存分配 - 高效的字符串操作 - 最小内存占用 - 针对嵌入式系统优化 ## 开源协议 本库为开源软件,可自由使用。请在源文件中保留原作者的版权声明。 ## 作者 作者:张彦欣 版权所有 © 2014-2024 ## 参与贡献 欢迎贡献代码!请随时提交拉取请求或创建问题报告错误和功能请求。 ## 版本历史 - 2015.07.29:初始发布 - 2020.03.18:增加缓冲区管理改进 - 2020.04.10:增强错误处理 - 2024.12.26:代码清理和文档更新