如何将QString拆分成QStringList
时间: 2024-05-17 17:16:13 浏览: 93
可以使用split()函数将QString拆分成QStringList。split()函数的用法是QString.split(separator, behavior),其中separator是要拆分的字符串,behavior是一个可选参数,用于指定是否过滤空字符串。例如,如果要将字符串str按逗号拆分成QStringList,可以使用以下代码:
```cpp
QString str = "a,,b,c";
QStringList list = str.split(",");
```
这将得到一个QStringList,其中包含拆分后的字符串元素:["a", "", "b", "c"]。如果要过滤掉空字符串,可以在split()函数中添加QString::SkipEmptyParts参数:
```cpp
QStringList list = str.split(",", QString::SkipEmptyParts);
```
这样就会得到一个过滤掉了空字符串的QStringList:["a", "b", "c"]。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Qt扫盲-QString使用总结](https://blog.csdn.net/qq_43680827/article/details/128203725)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [QString(详解)](https://blog.csdn.net/ligare/article/details/124495473)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文