好的 C++ 字符串操作库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1101599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:47:26  来源:igfitidea点击:

Good C++ string manipulation library

c++string

提问by Viet

I'm sorry for flaming std::string and std::wstring. They are quite limited and far from being thread safe. Performance wise, they are not that good too. I miss simple features:

我很抱歉燃烧 std::string 和 std::wstring。它们非常有限,远非线程安全。性能方面,它们也不是那么好。我想念简单的功能:

  1. Splitting a string into array/vector/list
  2. Simple & intuitive case-insensitive find & replace
  3. Support for i18n without worrying about string or wstring
  4. Conversion to and from int, float, double
  5. Conversion to and from UTF-8, UTF-16 & other encodings
  6. Thread-safe/reentrant
  7. Small footprint & no dependencies
  8. Highly portable & cross-platform
  1. 将字符串拆分为数组/向量/列表
  2. 简单直观的不区分大小写的查找和替换
  3. 支持 i18n,无需担心 string 或 wstring
  4. 与 int、float、double 的转换
  5. 与 UTF-8、UTF-16 和其他编码之间的转换
  6. 线程安全/可重入
  7. 占用空间小,无依赖性
  8. 高度便携和跨平台

I've found Qt QString to be useful and also found CBString http://bstring.sourceforge.net/bstrFAQ.shtml

我发现 Qt QString 很有用,还发现 CBString http://bstring.sourceforge.net/bstrFAQ.shtml

Any other suggestions & comparisons? Thank you.

还有其他建议和比较吗?谢谢你。

采纳答案by John T

The C++ String Algorithms Libraryfrom Boosthas pretty much all of the features you need.

C ++字符串算法库升压拥有几乎所有的功能,你需要的。

回答by Viet

The C++ String Toolkit (StrTk) Library is a free library that consists of robust, optimized and portable generic string processing algorithms and procedures for the C++ language. The library is designed to be easy to use and integrate within existing code.

C++ 字符串工具包 (StrTk) 库是一个免费库,它包含适用于 C++ 语言的稳健、优化和可移植的通用字符串处理算法和过程。该库旨在易于使用并集成到现有代码中。

The library has the following capabilities:

该库具有以下功能:

  • Generic string tokenizer and token iterators
  • Split routines
  • User specified delimiter and splitter policies (simple and regex based etc.)
  • Conversions between data and hex and base-64
  • In-place removal and replace routines
  • Wild-card matching and globing
  • Fast 2D token grid processing
  • Extensible string processing templates
  • 通用字符串标记器和标记迭代器
  • 拆分例程
  • 用户指定的分隔符和拆分器策略(简单和基于正则表达式等)
  • 数据与十六进制和 base-64 之间的转换
  • 就地拆除和更换例程
  • 通配符匹配和通配符
  • 快速 2D 令牌网格处理
  • 可扩展的字符串处理模板

and plenty more...

还有更多……

Compatible C++ Compilers:

兼容的 C++ 编译器:

  • GCC 4.0+
  • Intel C++ Compiler 9.0+
  • Microsoft Visual C++ 8.0+
  • Comeau C/C++ 4.1+
  • 海湾合作委员会 4.0+
  • 英特尔 C++ 编译器 9.0+
  • 微软 Visual C++ 8.0+
  • 科莫 C/C++ 4.1+

Source:

来源:

回答by Steven Sudit

I'm not sure I agree. Strings really shouldn't be thread-safe due to the overhead, except for reference-counting, if applicable. Most of the other functionality you want would turn strings into a garbage barge. Likewise, removing dependencies would remove their ability to work well with streams.

我不确定我是否同意。由于开销,字符串确实不应该是线程安全的,除非引用计数(如果适用)。您想要的大多数其他功能会将字符串变成垃圾驳船。同样,删除依赖项将删除它们与流一起工作的能力。

The one thing I'd suggest is that we could benefit from an immutable string class, particularly one that has no memory ownership or termination. I've written those before and they can be very helpful.

我建议的一件事是我们可以从不可变的字符串类中受益,特别是没有内存所有权或终止的字符串类。我以前写过这些,它们会很有帮助。

回答by LiraNuna

Bstring- Although I never tried it myself, the feature set and speed presented at their site. Under your choice of GPL or BSD license is also a good degree of freedom.

Bstring- 虽然我自己从未尝试过,但在他们的网站上展示了功能集和速度。根据你选择的 GPL 或 BSD 许可也是一个不错的自由度。

Also, the name suggests it's better so how can they lie? :)

此外,这个名字表明它更好,所以他们怎么能撒谎?:)

回答by rve

I found wxString convenient to use and it has many features. Although it is part of a bigger library (wxWidgets) and maybe just too big when you just want to use strings. It also works without GUI components when you just use wxBase which contains the wxString and a 'few' other components.

我发现 wxString 使用起来很方便,而且它有很多功能。虽然它是一个更大的库(wxWidgets)的一部分,而且当你只想使用字符串时它可能太大了。当您只使用包含 wxString 和“少数”其他组件的 wxBase 时,它​​也可以在没有 GUI 组件的情况下工作。

EDIT: here is a linkto the documentation. It accepts the standard functions of std::string and also a few others. I always find the BeforeFirst() and AfterFirst() very convenient when I have to parse some text. And it is really well documented.

编辑:这里是文档的链接。它接受 std::string 的标准函数以及其他一些函数。当我必须解析一些文本时,我总是发现 BeforeFirst() 和 AfterFirst() 非常方便。它确实有据可查。

回答by Benoit Blanchon

I highly recommend to use ICU's UnicodeString, as it support all common string manipulation functions, as well as i18n support.

我强烈建议使用ICUUnicodeString,因为它支持所有常见的字符串操作函数,以及 i18n 支持。

Don't know what ICU is? Here is an extract from Wikipedia:

不知道ICU是什么?以下是维基百科的摘录:

International Components for Unicode (ICU) is an open source project of mature C/C++ and Java libraries for Unicode support, software internationalization, and software globalization. ICU is widely portable to many operating systems and environments. It gives applications the same results on all platforms and between C, C++, and Java software. The ICU project is sponsored, supported, and used by IBM and many other companies.

International Components for Unicode (ICU) 是成熟的 C/C++ 和 Java 库的开源项目,用于支持 Unicode、软件国际化和软件全球化。ICU 可广泛移植到许多操作系统和环境中。它在所有平台上以及在 C、C++ 和 Java 软件之间为应用程序提供相同的结果。ICU 项目由 IBM 和许多其他公司赞助、支持和使用。

回答by jkeys

For conversion, you can always break down and use the C library cstdlib.

对于转换,您可以随时分解并使用 C 库 cstdlib。

#include <cstlib>
#include <iostream>

int main()
{
   std::string num;

   std::cin >> num;

   someFunc(atoi(num));
   someOtherFunc(atof(num));
   return 0;
}

atoi = ascii to integer atof = ascii to float

atoi = ascii 到整数 atof = ascii 到浮点数

As for find, use the STL function "find" defined under header algorithm, or find_first_of (or similar). I also believe you can initialize a vector of char's with an std::string, but that is conjecture.

至于find,使用header algorithm下定义的STL函数“ find”,或者find_first_of(或类似的)。我也相信你可以用 std::string 初始化一个字符向量,但这是猜想。