gpt4 book ai didi

c++ - 如何检查 QString 的第一个字符?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:08:07 24 4
gpt4 key购买 nike

我希望以下代码从价格中删除前导零(0.00 应减为 .00)

QString price1 = "0.00";
if( price1.at( 0 ) == "0" ) price1.remove( 0 );

这给了我以下错误:“错误:从‘const char [2]’到‘QChar’的转换不明确”

最佳答案

主要问题是 Qt 将 "0" 视为以 null 结尾的 ASCII 字符串,因此有关 const char[2] 的编译器消息。

此外,QString::remove() 有两个参数。所以你的代码应该是:

if( price1.at( 0 ) == '0' ) price1.remove( 0, 1 );

这在我的系统(Qt 4.7.3、VS2005)上构建并运行。

关于c++ - 如何检查 QString 的第一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8404872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com