1 2 Previous Next 16 Replies Latest reply: Nov 25, 2013 1:27 AM by Gen.Java RSS

    API required

    Gen.Java

      Hi all,

      Is there an API that returns true if one string contains any character that is in another string ?

      Example: s1="abcd", s2="c1". returns true because s1 has 'c' which is in s2.

      String.matches() can help but it tends to be a bit slow.

      Thank you


        • 1. Re: API required
          rp0428
          Is there an API that returns true if one string contains any character that is in another string ?

          Example: s1="abcd", s2="c1". returns true because s1 has 'c' which is in s2.

          String.matches() can help but it tends to be a bit slow.

          Slow? Slow compared to 'what'. Speed is relative; you have to compare it to something and, so far, you have NOTHING to compare it with.

           

          EVERY character of string1 has to be compared, one at a time, to EVERY character of string2 until you either find a match or have tested EVERY character of string1.

           

          How would you expect that to be any faster than what the 'matches' method will give you.

           

          You can do your own tests using the 'contains' method. Just iterate through string1, one character at a time and see if string2 'contains' that one character.

           

          Why don't you just start over and tell us what PROBLEM you are trying to solve? Then maybe we can suggest a better way to solve it.

          • 2. Re: API required
            EJP

            rp0428

             

            Please knock off the constant capitalization of every second word. It reads like shouting on the Internet. Use italics if you must, but don't overdo it.

             

            Moderator

            • 3. Re: API required
              rp0428

              Quit being ridiculous. It doesn't read like that at all. There is NO capitalization of every second word. Capitalization of a keyword is an effective way to emphasize it.

               

              This is NOT your personal forum. If you want to suggest forum guidelines you should register your opinions with Oracle. They own the forums are are SOLELY responsible for the guidelines that should be used.

               

              ------------------

              Updated 7:52 PM - to add links to forum guidelines

               

              I have reviewed the current guidelines for the forums and there is currently NO guideline such as what you seem to be proposing.

               

              Here is a link to the current WIKI so you can review them for yourself.

              https://wikis.oracle.com/display/Forums/Forums+FAQ

               

              And here is a link to Oracle's guidelines for moderators in case you would like to reread them.

              https://wikis.oracle.com/display/Forums/OTN+Community+Moderator+Guidelines

               

              Please post links to any other official Oracle guidelines if you are aware of any.

              • 4. Re: API required
                EJP

                I am suggesting a forum guideline. To you. It is generally accepted that capitalization = shouting on the Internet. I suggest that at the least you fail the 'not in good taste' test in the Terms of Use.

                • 5. Re: API required
                  Gen.Java

                  Thank you all for your advice.

                  Before I re-invent the wheel, I always look at the standard library if there is an API that I can use instead. In my post, I was looking for an equivalent API to the c# String.IndexOfAny(). I did not want to use String.matches() because I read in many blogs that regular expressions have certain amount of overhead, which I was trying to overcome. So it was a yes/no answer really, and if yes, then what is it? that is all to it.

                  • 6. Re: API required
                    gimbal2

                    Gen.Java wrote:

                     

                    I did not want to use String.matches() because I read in many blogs that regular expressions have certain amount of overhead, which I was trying to overcome.

                    Why? Computers are wicked fast, you're not even going to be able to measure the difference between using matches() and not using it. Unless you're trying to run this on some Android device.

                     

                    Don't fall into the trap of premature optimization; performance is NOT an issue until you have actual measurable performance issues. And then you start to think about optimizing, if it is necessary. Until that moment, let the JVM do the worrying about optimization, it is far better at it than you ever will be. But you have to write proper code to let it do its job, and if you start out trying to be clever yourself you will probably only hinder it.

                     

                    The Developer Insight Series, Part 1: Write Dumb Code -- Advice From Four Leading Java Developers

                    • 7. Re: API required
                      rp0428

                      So finally, a week later, you finally post the answer to what I first ask you:

                      Why don't you just start over and tell us what PROBLEM you are trying to solve? Then maybe we can suggest a better way to solve it.

                       

                      And the answer is:

                      I was looking for an equivalent API to the c# String.IndexOfAny().

                      Next time post information about the actual quesiont/issue when you first create the thread. You will get MUCH better answers.

                      • 8. Re: API required
                        Gen.Java

                        So, is that a YES or NO.

                         

                        Also, I would specially like to thank wizzle for providing solutions eventhough I haven't tried them yet. I will mark his answer as "Correct".

                        • 10. Re: API required
                          gimbal2

                          That was the result of Google for "java String indexOfAny", right?

                          • 11. Re: API required
                            Gen.Java

                            But this is not in the standard library. The user must load and install "apache", right?

                            • 12. Re: API required
                              gimbal2

                              No. You deploy whatever API you need with your application. You know how Java works don't you?

                              • 13. Re: API required
                                baftos

                                gimbal2 wrote:

                                 

                                That was the result of Google for "java String indexOfAny", right?

                                Right. How did you guess?

                                • 14. Re: API required
                                  gimbal2

                                  Yeah weird right! Its such a leap to make.

                                  1 2 Previous Next