Results 1 to 6 of 6

Thread: split camel case string

  1. #1
    Join Date
    Mar 2010
    Posts
    107
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default split camel case string

    Hi
    Say I have a string "TopBanana"
    What is the best way to split it into two strings "Top" and "Banana"
    I tried with QRegExp but i dont think im using it correctly
    Qt Code:
    1. QString s = QString("TopBanana");
    2.  
    3. QRegExp re = QRegExp("[A-Z}");
    4.  
    5. QStringList sl = s.split(re);
    To copy to clipboard, switch view to plain text mode 

    TIA

    Graham

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: split camel case string

    is "[A-Z}" a typo?
    Didn't you mean "[A-Z]"?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2010
    Posts
    18
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: split camel case string

    You wrote QRegExp("[A-Z}"); - it should be QRegExp("[A-Z]");

    You can try this:
    Qt Code:
    1. QString s = QString("TopBanana");
    2. QRegExp re1 = QRegExp("([A-Z])([a-z]*)");
    3. s.replace(re1, ";\\1\\2");
    4. QStringList sl = s.split(";");
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to majorwoody for this useful post:

    GrahamLabdon (2nd February 2011)

  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: split camel case string

    beside the typo, you have to use a positive lookahead:
    Qt Code:
    1. QString s = QString("TopBanana");
    2. QStringList sl = s.split(QRegExp("(?=[A-Z])"), QString::SkipEmptyParts);
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Mar 2010
    Posts
    107
    Thanks
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: split camel case string

    Hi thanks
    This solution works but the split gives me 3 strings in the list the first of which is empty

  7. #6
    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: split camel case string

    Quote Originally Posted by GrahamLabdon View Post
    This solution works but the split gives me 3 strings in the list the first of which is empty
    Yes, and that is the reason why I passed QString::SkipEmptyParts ad a second parameter to the split function!

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

    GrahamLabdon (2nd February 2011)

Similar Threads

  1. Replies: 7
    Last Post: 6th February 2017, 19:10
  2. How to split a string line
    By grsandeep85 in forum Newbie
    Replies: 5
    Last Post: 29th July 2009, 10:42
  3. How to split a string line
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2009, 10:28
  4. how to split a class ?
    By mimmo_kallon in forum Newbie
    Replies: 0
    Last Post: 8th April 2008, 11:37
  5. QString split()
    By ShaChris23 in forum Newbie
    Replies: 4
    Last Post: 3rd May 2007, 04:10

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.