Results 1 to 10 of 10

Thread: How to capitalize a string

  1. #1
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default How to capitalize a string

    Hi to all,
    I can't believe that, it's missing a capitalizing function in Qt!!!
    Does anyone suggest a quick way to do it?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to capitalize a string

    The doc it your friend. Use it! QString::toUpper()

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

    Cupidvogel (7th January 2016)

  4. #3
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to capitalize a string

    Quote Originally Posted by Lykurg View Post
    The doc it your friend. Use it! QString::toUpper()
    ... for the first char and ::toLower() for the other chars. Thanks, who is the next?

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to capitalize a string

    Quote Originally Posted by cydside View Post
    Thanks, who is the next?
    I hope you don't want to pull my leg. Where is the problem in combining that with normal c++ rules?
    Qt Code:
    1. QString capitalize(const QString &str)
    2. {
    3. QString tmp = str;
    4. // if you want to ensure all other letters are lowercase:
    5. tmp = tmp.toLower();
    6. tmp[0] = str[0].toUpper();
    7. return tmp;
    8. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to capitalize a string

    Quote Originally Posted by Lykurg View Post
    I hope you don't want to pull my leg. Where is the problem in combining that with normal c++ rules?
    Sorry, I don't mean to hurt your feelings. I'm just looking for a smartest solution(if exist) used by someone else!

  7. #6
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to capitalize a string

    Quote Originally Posted by cydside View Post
    ... for the first char and ::toLower() for the other chars. Thanks, who is the next?
    Uhm, the docs clearly state that TeXt turns into TEXT. If this isn't the case, either you have done something wrong or there's a bug in Qt. Since they have a QTestLib example built around this exact function, I am pretty sure you have done something wrong.

    Edit: I really should pay a bit more attention. The question really wasn't clear enough but you noticed it yourself after lykurg's reply...
    Last edited by franz; 18th July 2010 at 19:57.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  8. #7
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to capitalize a string

    There's no function to change the first character of a string to upper case because such a function makes little sense in many languages; it is a task specific mainly to Western languages and alphabets. In today's global, Unicode world the majority of readers would find such a function useless.

    If that's all you need, however, however, myString[0].toUpper() will get you there. Feel free to define a macro, or a function, or simply hard-code the solution wherever it's needed.

  9. #8
    Join Date
    Nov 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to capitalize a string

    You can use QFont::setCapitalization(). I guess the rationale is that it's a rendering step.

    Reid

  10. #9
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to capitalize a string

    Hi,

    I guess you want to capitalize all nouns in a sentence, as is common for titles in English.

    Use the split method of QString, using a space as separator. This gives you a list of strings. toUpper() each first character of each string in the stringlist. Then concatenate all the strings back together, putting a space in between them.

    You could add exceptions that the words 'and' and 'or' and such are not capitalized.

    Best regards,
    Marc

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to capitalize a string

    I usually do:
    Qt Code:
    1. str = str.left(1).toUpper()+str.mid(1);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    frankiefrank (7th November 2012)

Similar Threads

  1. std:string how to change into system:string?
    By yunpeng880 in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 08:51
  2. Int to String - manipulating string
    By mickey in forum General Programming
    Replies: 6
    Last Post: 5th November 2007, 20:11
  3. Problem with std::string's
    By Rayven in forum General Programming
    Replies: 1
    Last Post: 17th July 2007, 18:29
  4. how to get a part of the string
    By probine in forum Newbie
    Replies: 2
    Last Post: 4th December 2006, 14:37
  5. String value
    By Gayathri in forum Newbie
    Replies: 2
    Last Post: 21st November 2006, 05:44

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.