qt - 如何从 Qstring 中获取第一个单词

标签 qt qstring qregexp

我想得到一个 Qstring 的第一个词.

例如 String1 = "Read from file1" .我要提取string2 = "Read" .
我想根据空格提取子字符串。

如果我在 string1 中遇到第一个空格,我需要string1的那部分至 string2 .

最佳答案

使用QString的拆分功能通过这种方式:

QString firstWord = string1.split(" ").at(0);

如果字符串中没有空格,则返回整个字符串。

关于qt - 如何从 Qstring 中获取第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21604722/

相关文章:

c++ - C++中的QRegExp捕获部分字符串

c++ - QRegExp 仅解析以特定字符开头的字符串

c++ - QVector2D* 大小未知

c++ - 在小部件中居中 QGraphicsView

c++ - 将 QString 转换为 QJsonArray

qt - 如何使用 QPlaintTextEdit 有效地创建固定宽度的列记录器?

c++ - QProgressBar 和繁重的任务

c++ - 如何使用 Qt 5.6 在高 dpi 上获得清晰的 UI?

c++ - 如何在 Qt 小部件中使用 QStringView(或 QStringRef)?

c++ - 双括号QString之间只选一个选项