Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the first letter of the string?
Qt 6.11 is out! See what's new in the release blog

How to get the first letter of the string?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 5.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aurora
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cincirin
      wrote on last edited by
      #2

      @
      ((line.constData())[0]).toAscii()
      @

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #3

        Simple answer: line.at(0)

        Complex answer: What do you understand a letter to be?

        With unicode one QChar can be any one of these:

        • something simple as e.g. 'A'.
        • fun stuff like 'LATIN SMALL LIGATURE FFI' (U+FB03) which would most likely be counted as 3 letters by most people
        • only part of one complex glyph (the drawing commonly referred to as "letter"). E.g. you could write 'Ö' (LATIN CAPITAL LETTER O WITH DIAERESIS, U+00D6) as LATIN CAPITAL LETTER O, U+004F followed by COMBINING DIAERESIS, U+0308
        • or even just "half a unicode codepoint" when surrogate pairs are in use, which extend the 16bit unicode space that is addressable directly by UTF-16 used in QString by combining 2 UTF-16 characters.
        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by
          #4

          operator[] or at()
          index number is of course zero

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aurora
            wrote on last edited by
            #5

            Thanks a lot for all of u...:)
            i did like this
            @
            std::cout<<"the first word of the line :"<<line.at(0).toAscii()<<endl;
            @

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved