PDA

View Full Version : how to get the first letter of the string?



aurora
20th December 2011, 15:23
hi,
Is there any standard function(or way) to get the first letter of a given string?
I tried splitting the string, but that didnt work..


QStringList lineList=line.split(""); //the "line", i read it before from a file
std::cout<<"the first word of the line :"<<lineList.at(0).toStdString()<<endl;

If anybody knows, plz tell me...
Thank u

fullmetalcoder
20th December 2011, 18:04
Is there any standard function(or way) to get the first letter of a given string?

QString s("are you a troll?");
std::cout << s.at(0); << std::endl; // prints 'a' :rolleyes:

If however you want the first word, as the comment in your code snippet suggest, you could try this :

QString("read the docs luke").split(QRegExp("\\W+"), QString::SkipEmptyParts).at(0);

aurora
21st December 2011, 05:13
Thanks a lot.:)
i did like this


std::cout<<"the first word of the line :"<<line.at(0).toAscii()<<endl;

ChrisW67
21st December 2011, 06:18
... which is the first character of the line and not the first word.

aurora
21st December 2011, 09:35
ya...:)
first word can get by splitting the line by space , then storing in a list right ?

Spitfire
21st December 2011, 15:12
If you're going to use only the first word there's no point in breaking whole string and storing it in a list.

QString str("few words here.");
qDebug() << str.mid( 0, str.indexOf(" ", 0 ) ); // prints 'few'

aurora
22nd December 2011, 04:58
If you're going to use only the first word there's no point in breaking whole string and storing it in a list.

QString str("few words here.");
qDebug() << str.mid( 0, str.indexOf(" ", 0 ) ); // prints 'few'

Thank u...:)
i was not knowing this...