String .contains()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    String .contains()

    Hi all,

    I was rather suprised to find that the String java api doesn't contain String.contains (char aChar) method. It has a similar method for a charsequence but not one for what i need.

    So unless someone tells me im wrong I ve come up with a another way.

    If i have a char aChar = 'x'

    then a String aString = "test"

    if i do this aString.indexOf (aChar);

    will I either get the index number of aChar or -1 if it isn't in there??

    So then i can return false if -1 is returned???

    Regards

    Brendan
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Yep, you can also use a String as an argument for the indexOf() method, e.g.
    "foobar".indexO f("oba") == 2

    kind regards,

    Jos

    Comment

    • brendanmcdonagh
      New Member
      • Nov 2007
      • 153

      #3
      cheers Jos,

      I'm starting to think for myself!!

      Comment

      • brendanmcdonagh
        New Member
        • Nov 2007
        • 153

        #4
        I'm not so smart after all....

        aString = "three"
        aChar = 'e'

        What would be the return of aString.indexOf (aChar) because of multiple occurences?

        Comment

        • brendanmcdonagh
          New Member
          • Nov 2007
          • 153

          #5
          Code:
              private void setStringBuffer()
              {
                  int test = aString.indexOf(aChar);
                  while(test != -1)
                  {
                      aStringBuffer.setCharAt(test, aChar);
                      test = aString.indexOf(test + 1, aChar);
                  }
                      
                      
              }
          I think this is the way to check for multiple occurences and them to a stringbuffer. If it is do i need the +1 after test to start looking for more??

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by brendanmcdonagh
            Code:
                private void setStringBuffer()
                {
                    int test = aString.indexOf(aChar);
                    while(test != -1)
                    {
                        aStringBuffer.setCharAt(test, aChar);
                        test = aString.indexOf(test + 1, aChar);
                    }
                        
                        
                }
            I think this is the way to check for multiple occurences and them to a stringbuffer. If it is do i need the +1 after test to start looking for more??
            Yep, otherwise the last found char is found again and again. A bit of for-loop
            crafting helps you out:

            Code:
            int nofCharInString(char aChar, String aString) { // assume String non-null
               int count= 0;
               for (int i= -1; (i= aString.indexOf(aChar, i+1)) >= 0; count++);
               return count;
            }
            kind regards,

            Jos

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by brendanmcdonagh
              Code:
                  private void setStringBuffer()
                  {
                      int test = aString.indexOf(aChar);
                      while(test != -1)
                      {
                          aStringBuffer.setCharAt(test, aChar);
                          test = aString.indexOf(test + 1, aChar);
                      }
                          
                          
                  }
              I think this is the way to check for multiple occurences and them to a stringbuffer. If it is do i need the +1 after test to start looking for more??
              The API specs for the String class will tell you exactly what happens with that String.indexOf method.

              P.S Also google StringBuilder vs StringBuffer.

              Comment

              • brendanmcdonagh
                New Member
                • Nov 2007
                • 153

                #8
                Hi again,

                I have to use StringBuffer on this course but also know about StringBuilder from last course.

                Please do not think i am just wanting someone to do my work for me as I don't, I get a buzz out of writing methods that have not required me consulting api or this website, it's just sometimes i can look forever and get no where without someones input, which I learn massively from.

                I have a error

                Exception in thread "main" java.lang.Strin gIndexOutOfBoun dsException: String index out of range: 1
                at java.lang.Strin gBuffer.setChar At(StringBuffer .java:211)
                at tma01q3.Coder.s etFeedback(Code r.java:64)
                at tma01q3.Coder.u pdateState(Code r.java:79)
                at tma01q3.Coder.t urn(Coder.java: 104)
                at tma01q3.TestGam e.main(TestGame .java:38)

                which i think has something to do with this method

                Code:
                    private void setFeedback()
                    {
                        int test = codeWord.indexOf(currentGuess);
                        System.out.println(test);
                        while(test != -1)
                        {
                            feedback.setCharAt(test, currentGuess);
                            test = codeWord.indexOf(test + 1, currentGuess);
                            
                        }
                            
                         
                    }
                Anybody see whats wrong, I am inputting a letter which is assigned to currentGuess, then I have a look at codeWord to see if it contains the letter(currentG uess) and if so setChar() of StringBuffer(fe edback)

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  From the API specs, setCharAt
                  Originally posted by API Specs
                  Throws:
                  IndexOutOfBound sException - if index is negative or greater than or equal to length().

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by brendanmcdonagh
                    Hi again,

                    I have to use StringBuffer on this course but also know about StringBuilder from last course.

                    Please do not think i am just wanting someone to do my work for me as I don't, I get a buzz out of writing methods that have not required me consulting api or this website, it's just sometimes i can look forever and get no where without someones input, which I learn massively from.

                    I have a error

                    Exception in thread "main" java.lang.Strin gIndexOutOfBoun dsException: String index out of range: 1
                    at java.lang.Strin gBuffer.setChar At(StringBuffer .java:211)
                    at tma01q3.Coder.s etFeedback(Code r.java:64)
                    at tma01q3.Coder.u pdateState(Code r.java:79)
                    at tma01q3.Coder.t urn(Coder.java: 104)
                    at tma01q3.TestGam e.main(TestGame .java:38)

                    which i think has something to do with this method

                    Code:
                        private void setFeedback()
                        {
                            int test = codeWord.indexOf(currentGuess);
                            System.out.println(test);
                            while(test != -1)
                            {
                                feedback.setCharAt(test, currentGuess);
                                test = codeWord.indexOf(test + 1, currentGuess);
                                
                            }
                                
                             
                        }
                    Anybody see whats wrong, I am inputting a letter which is assigned to currentGuess, then I have a look at codeWord to see if it contains the letter(currentG uess) and if so setChar() of StringBuffer(fe edback)
                    Suppose you have an empty StringBuffer and you find a character at position three
                    (test == 3). You can't set a character at position three in an empty StringBuffer.

                    kind regards,

                    Jos

                    Comment

                    • brendanmcdonagh
                      New Member
                      • Nov 2007
                      • 153

                      #11
                      So are stringbuffer and string different in that first letter in one is index 0 and first letter in another is 1??

                      As I set the capacity of stringbuffer to same as String

                      feedback = new StringBuffer(co de.length());

                      thanks for keeping paience

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by brendanmcdonagh
                        So are stringbuffer and string different in that first letter in one is index 0 and first letter in another is 1??

                        As I set the capacity of stringbuffer to same as String

                        feedback = new StringBuffer(co de.length());

                        thanks for keeping paience
                        The capacity is of no consequence. It's the length which matters. See quote from the specs that I posted above again.

                        Comment

                        • brendanmcdonagh
                          New Member
                          • Nov 2007
                          • 153

                          #13
                          Ok, checked it again and there is no setLength() for stringBuffer, so unless someone can show me different I need to instantinate the StringBuffer with _ (underscore) as many times as there are characters in the String?? so then when i come to setChar() there won't be any out of bounds because of lack of lentgh??

                          Any ideas how, please:(

                          Comment

                          • brendanmcdonagh
                            New Member
                            • Nov 2007
                            • 153

                            #14
                            There is a setLength() you guys are the best!

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by brendanmcdonagh
                              Ok, checked it again and there is no setLength() for stringBuffer, so unless someone can show me different I need to instantinate the StringBuffer with _ (underscore) as many times as there are characters in the String?? so then when i come to setChar() there won't be any out of bounds because of lack of lentgh??

                              Any ideas how, please:(
                              You need to initialize it with the String value itself not with just the String's length.
                              Check the constructors for it in the .... API specs.

                              Comment

                              Working...