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. QString - secondIndexOf
QtWS25 Last Chance

QString - secondIndexOf

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 878 Views
  • 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.
  • J Offline
    J Offline
    jellyv
    wrote on last edited by
    #1

    I'm trying to get the second index of a character (;) in QString, but the only voids I'm aware of are
    Qstring.indexOf() and QString.lastIndexOf() .
    Is there any way to find the second index of a semicolon?
    For example, Qstring is: file001;15-02-2019;Accounting;Management; .
    I'm trying to get the index of 2nd semicolon (the one after 2019). How is that possible?

    J.HilkJ 1 Reply Last reply
    0
    • J jellyv

      I'm trying to get the second index of a character (;) in QString, but the only voids I'm aware of are
      Qstring.indexOf() and QString.lastIndexOf() .
      Is there any way to find the second index of a semicolon?
      For example, Qstring is: file001;15-02-2019;Accounting;Management; .
      I'm trying to get the index of 2nd semicolon (the one after 2019). How is that possible?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      @jellyv indexOf() excepts a start index as 2nd parameter and the documentation shows it
      https://doc.qt.io/qt-5/qstring.html#indexOf

      so

      int firstIndex = myString.index(';');
      int 2ndIndex = myString.index(';',firstIndex+1);
      

      that said, what do you want to do with the index. If you want to extract the strings between ';'

      you could use split or splitref for example.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      J 2 Replies Last reply
      2
      • J.HilkJ J.Hilk

        @jellyv indexOf() excepts a start index as 2nd parameter and the documentation shows it
        https://doc.qt.io/qt-5/qstring.html#indexOf

        so

        int firstIndex = myString.index(';');
        int 2ndIndex = myString.index(';',firstIndex+1);
        

        that said, what do you want to do with the index. If you want to extract the strings between ';'

        you could use split or splitref for example.

        J Offline
        J Offline
        jellyv
        wrote on last edited by
        #3

        @J-Hilk
        I've tried int 2ndIndex = myString.index(';',firstIndex+1); method but it's useless when you try to find 3rd , 4th and other indexes of a character.
        Split is what I actually needed in my case, thank you!

        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @jellyv indexOf() excepts a start index as 2nd parameter and the documentation shows it
          https://doc.qt.io/qt-5/qstring.html#indexOf

          so

          int firstIndex = myString.index(';');
          int 2ndIndex = myString.index(';',firstIndex+1);
          

          that said, what do you want to do with the index. If you want to extract the strings between ';'

          you could use split or splitref for example.

          J Offline
          J Offline
          jellyv
          wrote on last edited by
          #4

          @J-Hilk UPDATE:
          section actually is the best way to get a section of a string which is separated by characters

          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