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. Pull apart String
Forum Updated to NodeBB v4.3 + New Features

Pull apart String

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.0k Views 2 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.
  • JulianJ Offline
    JulianJ Offline
    Julian
    wrote on last edited by Julian
    #1

    Hello. How Can I pull apart a section of QString?

    For example,

    QString chain; 
    chain.append("The real"):
    
    

    I want to have two QString, one with "The" and the other with "real". What I have tried it's to find first the space between words

    QString chain; 
    QString aux;
    QString aux2;
    chain.append("The real"):
    
    
    //let's find space's position
    while(chain[position]!=" ")
    {
    position++;
    } 
    
    //Now, make the two QString
    for(int i=0; i<position; i++) 
    {
    aux.push_back(chain[i]); 
    }
    
    for(int i=position+1;i<chain.length(); i++) 
    { 
    aux2.push_bach(chain[i]);
    
    //Now aux must be "The " and aux2 must be "real"
    

    But this code do not work. Why.

    1 Reply Last reply
    0
    • 6thC6 Offline
      6thC6 Offline
      6thC
      wrote on last edited by
      #2

      Well, you have puxh_bach and push_back in the same code example. I'm not internationally versed so if that's german or something and the compiler is cool with building that... dunno.

      Sure there is push back and all but I am not sure how much fun you'll have trying to access QStrings like Char arrays? Your codes' intention and purpose is not obvious. Not sure the rule for having the first one include the space and the second not either.

      I think it would be helpful for you to know of this:
      If you just want the data between spaces (or another delimiter) you could use like something like:
      QString sData = getData();
      QStringList stringList = sData.split(" ", QString::SplitBehavior::KeepEmptyParts);

      Hopefully helpful...

      1 Reply Last reply
      2
      • JulianJ Offline
        JulianJ Offline
        Julian
        wrote on last edited by
        #3

        Thank. QString::split() it great!

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Like @6thC already said, split() is your friend. For more info on string handling, I recommend reading QString's documentation page, it's great: QString Class :-)

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Please consider splitRef() instead of split(). It does not make copies. Also if you only want one part of the string take a look at section().

            1 Reply Last reply
            3

            • Login

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