How to view the first character of a QString?

I want the following code to remove the initial zero from the price (0.00 should be reduced to .00)

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

This gives me the following error: "error: conversion from" const char [2] to "QChar is ambiguous"

+4
source share
3 answers

The main problem is that Qt sees "0" as an ASCII string with zero termination, so the compiler reports about const char[2] .

In addition, QString::remove() accepts two arguments. Thus, the code should be:

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

This builds and works on my system (Qt 4.7.3, VS2005).

+6
source

Try the following:

 price1.at( 0 ) == '0' ? 
+4
source

The problem is that the 'at' function returns a QChar , which is an object that cannot be matched to the native char / string "0". You have several options, but I'll just put two here:

 if( price1.at(0).toAscii() == '0') 

or

 if( price1.at(0).digitValue() == 0) 

digitValue returns -1 if char is not a digit.

+2
source

Source: https://habr.com/ru/post/1384976/


All Articles