Results 1 to 7 of 7

Thread: how to get the first letter of the string?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default how to get the first letter of the string?

    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..

    Qt Code:
    1. QStringList lineList=line.split(""); //the "line", i read it before from a file
    2. std::cout<<"the first word of the line :"<<lineList.at(0).toStdString()<<endl;
    To copy to clipboard, switch view to plain text mode 
    If anybody knows, plz tell me...
    Thank u

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get the first letter of the string?

    Quote Originally Posted by aurora View Post
    Is there any standard function(or way) to get the first letter of a given string?
    Qt Code:
    1. QString s("are you a troll?");
    2. std::cout << s.at(0); << std::endl; // prints 'a'
    To copy to clipboard, switch view to plain text mode 


    If however you want the first word, as the comment in your code snippet suggest, you could try this :
    Qt Code:
    1. QString("read the docs luke").split(QRegExp("\\W+"), QString::SkipEmptyParts).at(0);
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  3. The following user says thank you to fullmetalcoder for this useful post:

    aurora (21st December 2011)

  4. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get the first letter of the string?

    Thanks a lot.
    i did like this
    Qt Code:
    1. std::cout<<"the first word of the line :"<<line.at(0).toAscii()<<endl;
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to get the first letter of the string?

    ... which is the first character of the line and not the first word.

  6. #5
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get the first letter of the string?

    ya...
    first word can get by splitting the line by space , then storing in a list right ?

  7. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to get the first letter of the string?

    If you're going to use only the first word there's no point in breaking whole string and storing it in a list.
    Qt Code:
    1. QString str("few words here.");
    2. qDebug() << str.mid( 0, str.indexOf(" ", 0 ) ); // prints 'few'
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to Spitfire for this useful post:

    aurora (22nd December 2011)

  9. #7
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get the first letter of the string?

    Quote Originally Posted by Spitfire View Post
    If you're going to use only the first word there's no point in breaking whole string and storing it in a list.
    Qt Code:
    1. QString str("few words here.");
    2. qDebug() << str.mid( 0, str.indexOf(" ", 0 ) ); // prints 'few'
    To copy to clipboard, switch view to plain text mode 
    Thank u...
    i was not knowing this...

Similar Threads

  1. space in sql value is replaced by letter T
    By Gunnar in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2010, 19:25
  2. How to get the Drive letter
    By newb in forum Qt Programming
    Replies: 10
    Last Post: 10th June 2010, 13:59
  3. Shourtcut for letter
    By swrer in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2010, 10:29
  4. How to recognize a letter from a Qstring?
    By luffy27 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2007, 19:22
  5. adjust Letter Spacing
    By niko in forum Newbie
    Replies: 3
    Last Post: 16th August 2006, 10:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.