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 search for a specific character in a QString
QtWS25 Last Chance

How to search for a specific character in a QString

Scheduled Pinned Locked Moved Solved General and Desktop
30 Posts 9 Posters 42.8k 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.
  • T Offline
    T Offline
    tomy
    wrote on 7 Feb 2017, 22:15 last edited by
    #1

    Hi,

    Consider we have a QString and we want to search for a character in it.

    What method is best for it and what does it return when that character is found or not please?

    J 1 Reply Last reply 8 Feb 2017, 05:37
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Feb 2017, 22:27 last edited by
      #2

      Hi,

      What about QString::contains ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply 8 Feb 2017, 09:30
      5
      • A Offline
        A Offline
        Artmetic
        wrote on 7 Feb 2017, 22:44 last edited by
        #3

        or

        bool ClassName::containLetter(QChar letter, QString myString)
        {
        foreach(QChar char, myString)
        if(letter== char)
        return true;
        return false;
        }

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 7 Feb 2017, 22:53 last edited by
          #4

          @Artmetic why reinvent the wheel when the function is already available ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • A Offline
            A Offline
            Artmetic
            wrote on 7 Feb 2017, 22:55 last edited by
            #5

            Yes it is avaible, but everything depends of the problem. The use of your solution is probably optimalized and faster, but of course it depends :)

            1 Reply Last reply
            0
            • T tomy
              7 Feb 2017, 22:15

              Hi,

              Consider we have a QString and we want to search for a character in it.

              What method is best for it and what does it return when that character is found or not please?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 8 Feb 2017, 05:37 last edited by
              #6

              @tomy Taking a short look at Qt documentation you would be able to answer your question by yourself...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S SGaist
                7 Feb 2017, 22:27

                Hi,

                What about QString::contains ?

                T Offline
                T Offline
                tomy
                wrote on 8 Feb 2017, 09:30 last edited by tomy 2 Aug 2017, 09:33
                #7

                @SGaist
                Good solution, but what if we want to know "the index of that QChar" if exists?

                @jsulm
                Good thanks. How should I look for a specific method while apparently there are thousands methods on Qt documentations? For example I want the index of a specific char in a string (if that exists), what is an optimized way to look for it?

                PS: Of course I can use a simple for loop but when there is method already, why increase lines of code by loops!

                J J 2 Replies Last reply 8 Feb 2017, 09:36
                0
                • T tomy
                  8 Feb 2017, 09:30

                  @SGaist
                  Good solution, but what if we want to know "the index of that QChar" if exists?

                  @jsulm
                  Good thanks. How should I look for a specific method while apparently there are thousands methods on Qt documentations? For example I want the index of a specific char in a string (if that exists), what is an optimized way to look for it?

                  PS: Of course I can use a simple for loop but when there is method already, why increase lines of code by loops!

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 8 Feb 2017, 09:36 last edited by
                  #8

                  @tomy

                  the answer would be

                  int QString::indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
                  

                  ;)

                  straight from the docu, 10 seconds search. Once you have some kind of routine, the docu is realy fast and good to navigate.


                  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.

                  1 Reply Last reply
                  1
                  • T Offline
                    T Offline
                    tomy
                    wrote on 8 Feb 2017, 09:51 last edited by
                    #9

                    Thanks, but it does not work OK always. For example consider these:

                    double d = 12000000;
                    QString s = QString::number(d);
                     int point = s.indexOf('.');
                     lineEdit -> setText(QString::number(point));
                    

                    The result is 1!
                    Using this one too:

                    double d = 12000000;
                    QString s;
                    QTextStream ss(&s);
                    ss << d;
                    int point = s.indexOf('.')
                    lineEdit -> setText(QString::number(point));
                    

                    The result is 1 again!

                    matthew.kuiashM 1 Reply Last reply 8 Feb 2017, 09:58
                    0
                    • T tomy
                      8 Feb 2017, 09:51

                      Thanks, but it does not work OK always. For example consider these:

                      double d = 12000000;
                      QString s = QString::number(d);
                       int point = s.indexOf('.');
                       lineEdit -> setText(QString::number(point));
                      

                      The result is 1!
                      Using this one too:

                      double d = 12000000;
                      QString s;
                      QTextStream ss(&s);
                      ss << d;
                      int point = s.indexOf('.')
                      lineEdit -> setText(QString::number(point));
                      

                      The result is 1 again!

                      matthew.kuiashM Offline
                      matthew.kuiashM Offline
                      matthew.kuiash
                      wrote on 8 Feb 2017, 09:58 last edited by
                      #10

                      @tomy Dude, the answer "1" is spot on accurate.

                      If you display the result of QString::number(12000000) you'll get "1.2e+07" == 1.2 * 10^7 == 1.2 * 10000000 == 12000000.

                      Therefore the '.' is the second character e.g. index '1'.

                      Try QString::number(d, 'f', 0). That should work as it will output 12000000 and s.indexof('.') will return -1 (if that's what you want).

                      The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

                      1 Reply Last reply
                      1
                      • T Offline
                        T Offline
                        tomy
                        wrote on 8 Feb 2017, 10:06 last edited by
                        #11

                        There will not be any dot in other numbers, say, 12.21! Because we have used '0' as the precision!

                        matthew.kuiashM 1 Reply Last reply 8 Feb 2017, 10:12
                        0
                        • T tomy
                          8 Feb 2017, 10:06

                          There will not be any dot in other numbers, say, 12.21! Because we have used '0' as the precision!

                          matthew.kuiashM Offline
                          matthew.kuiashM Offline
                          matthew.kuiash
                          wrote on 8 Feb 2017, 10:12 last edited by matthew.kuiash 2 Aug 2017, 12:16
                          #12

                          @tomy Precisely. Your example was of an integer number, albeit packed into a double (there is adequate precision for your example)

                          If you ask for extra precision you will get extra '0's after the decimal point. I don't know if this is what you want.

                          What is it you are trying to do?! If you are justing searching for characters use indexOf. Is this is something to do with figuring out how many digits are required to display a number? In which case use math functions such as floor, ceiling and log10. If you want to know about the fractional parts use X-floor(X).

                          The example you gave does work. Just not the way you want it too and that was not what you originally asked for.

                          The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

                          T 1 Reply Last reply 8 Feb 2017, 16:58
                          1
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 8 Feb 2017, 10:33 last edited by
                            #13

                            Also, why not use a QDoubleSpinBox to show your numbers ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • T tomy
                              8 Feb 2017, 09:30

                              @SGaist
                              Good solution, but what if we want to know "the index of that QChar" if exists?

                              @jsulm
                              Good thanks. How should I look for a specific method while apparently there are thousands methods on Qt documentations? For example I want the index of a specific char in a string (if that exists), what is an optimized way to look for it?

                              PS: Of course I can use a simple for loop but when there is method already, why increase lines of code by loops!

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 8 Feb 2017, 12:15 last edited by
                              #14

                              @tomy How? Well, you know it is QString so take a look at its documentation. QString doesn't have "thousands" of methods.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • matthew.kuiashM matthew.kuiash
                                8 Feb 2017, 10:12

                                @tomy Precisely. Your example was of an integer number, albeit packed into a double (there is adequate precision for your example)

                                If you ask for extra precision you will get extra '0's after the decimal point. I don't know if this is what you want.

                                What is it you are trying to do?! If you are justing searching for characters use indexOf. Is this is something to do with figuring out how many digits are required to display a number? In which case use math functions such as floor, ceiling and log10. If you want to know about the fractional parts use X-floor(X).

                                The example you gave does work. Just not the way you want it too and that was not what you originally asked for.

                                T Offline
                                T Offline
                                tomy
                                wrote on 8 Feb 2017, 16:58 last edited by
                                #15

                                @matthew.kuiash

                                What I need is showing a double result. It may be a very small negative/positive number (say, -0.000000001234) or a very big positive/negative number (say, +2500000000036540). It's range I should cover by code.

                                I only need the 'number of' digits after the decimal point. Then I will use another method to use 'that number' precision for the output.

                                @SGaist:
                                Very good info, thanks.
                                But the lineEdit there, is used to "show" the result of a calculation. (My calculator works fine but I just want to make it more precise).
                                QDoubleSpinBox has two buttons and works in essence like an editable cadre, while I just need "to show" the result (and also save the current result for the next expressions).

                                matthew.kuiashM Z 2 Replies Last reply 8 Feb 2017, 21:57
                                0
                                • VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on 8 Feb 2017, 17:33 last edited by VRonin
                                  #16
                                  double d = 12000000.0;
                                  QString s = QString::number(d,'f',std::numeric_limits<decltype(d)>::max_digits10);
                                  if(s.contains('.')){
                                  int i=s.size()-1;
                                  for(;s.at(i)=='0';--i){}
                                  s=s.left(i+ (s.at(i)=='.' ? 0:1));
                                  }
                                  

                                  Edit:

                                  alternative:

                                  // needs #include <cmath>
                                  double d = 12000000.0;
                                  double junk;
                                  int precision=0;
                                  for(double tester=d;!qFuzzyIsNull(std::modf(tester,&junk));++precision,tester*=10.0){}
                                  QString s = QString::number(d,'f',precision);
                                  

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  T 1 Reply Last reply 8 Feb 2017, 19:54
                                  3
                                  • VRoninV VRonin
                                    8 Feb 2017, 17:33
                                    double d = 12000000.0;
                                    QString s = QString::number(d,'f',std::numeric_limits<decltype(d)>::max_digits10);
                                    if(s.contains('.')){
                                    int i=s.size()-1;
                                    for(;s.at(i)=='0';--i){}
                                    s=s.left(i+ (s.at(i)=='.' ? 0:1));
                                    }
                                    

                                    Edit:

                                    alternative:

                                    // needs #include <cmath>
                                    double d = 12000000.0;
                                    double junk;
                                    int precision=0;
                                    for(double tester=d;!qFuzzyIsNull(std::modf(tester,&junk));++precision,tester*=10.0){}
                                    QString s = QString::number(d,'f',precision);
                                    
                                    T Offline
                                    T Offline
                                    tomy
                                    wrote on 8 Feb 2017, 19:54 last edited by tomy 2 Aug 2017, 19:55
                                    #17

                                    @VRonin
                                    Thank you.

                                    Both have flaws. But also both have high level of precision.
                                    I think the flaws belong to the way numbers are stored in registers. And I think there is no completely (100%) accurate calculator in the world because of that.

                                    I chose the latter. Thanks for your help. I'm appreciative.

                                    1 Reply Last reply
                                    0
                                    • T tomy
                                      8 Feb 2017, 16:58

                                      @matthew.kuiash

                                      What I need is showing a double result. It may be a very small negative/positive number (say, -0.000000001234) or a very big positive/negative number (say, +2500000000036540). It's range I should cover by code.

                                      I only need the 'number of' digits after the decimal point. Then I will use another method to use 'that number' precision for the output.

                                      @SGaist:
                                      Very good info, thanks.
                                      But the lineEdit there, is used to "show" the result of a calculation. (My calculator works fine but I just want to make it more precise).
                                      QDoubleSpinBox has two buttons and works in essence like an editable cadre, while I just need "to show" the result (and also save the current result for the next expressions).

                                      matthew.kuiashM Offline
                                      matthew.kuiashM Offline
                                      matthew.kuiash
                                      wrote on 8 Feb 2017, 21:57 last edited by
                                      #18

                                      @tomy OK. You have plenty to go on here but I'll just caution you that the number of digits after the decimal point can be extremely high for doubles.

                                      The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

                                      T 1 Reply Last reply 9 Feb 2017, 05:25
                                      0
                                      • matthew.kuiashM matthew.kuiash
                                        8 Feb 2017, 21:57

                                        @tomy OK. You have plenty to go on here but I'll just caution you that the number of digits after the decimal point can be extremely high for doubles.

                                        T Offline
                                        T Offline
                                        tomy
                                        wrote on 9 Feb 2017, 05:25 last edited by tomy 2 Sept 2017, 05:27
                                        #19

                                        @matthew.kuiash
                                        I have used as high precision as possible for doubles using VRonin's code.
                                        I think I'm at the end. The differences between what we expect and what is shown using the code may happen for low of high precisions because of the difference of the way we and the machine calculate expressions.

                                        1 Reply Last reply
                                        0
                                        • T tomy
                                          8 Feb 2017, 16:58

                                          @matthew.kuiash

                                          What I need is showing a double result. It may be a very small negative/positive number (say, -0.000000001234) or a very big positive/negative number (say, +2500000000036540). It's range I should cover by code.

                                          I only need the 'number of' digits after the decimal point. Then I will use another method to use 'that number' precision for the output.

                                          @SGaist:
                                          Very good info, thanks.
                                          But the lineEdit there, is used to "show" the result of a calculation. (My calculator works fine but I just want to make it more precise).
                                          QDoubleSpinBox has two buttons and works in essence like an editable cadre, while I just need "to show" the result (and also save the current result for the next expressions).

                                          Z Offline
                                          Z Offline
                                          zapprox
                                          wrote on 9 Feb 2017, 06:43 last edited by
                                          #20

                                          Hi @tomy. QDoubleSpinBox can be used as you mentioned . First, to hide buttons call QAbstractSpinBox::setButtonSymbols(ButtonSymbols bs) and second call QAbstractSpinBox::setReadOnly(bool r).
                                          Have a nice day.

                                          VRoninV T 2 Replies Last reply 9 Feb 2017, 08:01
                                          0

                                          1/30

                                          7 Feb 2017, 22:15

                                          • Login

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