String .contains()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #16
    After initializing with the String itself, you can then use replaceAll to change every character to underscore.

    Comment

    • brendanmcdonagh
      New Member
      • Nov 2007
      • 153

      #17
      There isn't a replaceAll method for StringBuffer??

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #18
        ... or use an ordinary regular expression for that purpose:

        Code:
        String replaceNonChar(String s, char aChar) {
           return s.replaceAll("[^"+aChar+"]", "_");
        }
        kind regards,

        Jos

        Comment

        • brendanmcdonagh
          New Member
          • Nov 2007
          • 153

          #19
          ok im back to the reason i started this thread as my method isn't doing the job,

          Code:
          private void setFeedback()
              {
          
                  int test = codeWord.indexOf(currentGuess);
                  
                  while(test != -1)
                  {
                      feedback.setCharAt(test, currentGuess);
                      test = codeWord.indexOf(test, currentGuess);
                      
                  }
                      
                   
              }
          It is only entering into the stringbuffer the first occurence of currentGuess, so if three was codeword, stringbuffer is only having _ _ _ e _ added to it when e is current guess.

          I have tried with test and test + 1 which api says is where to start check from.

          I thought ii'd sorted this one!

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #20
            Why not just use the replaceAll method on the toString of that StringBuffer?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #21
              Originally posted by r035198x
              Why not just use the replaceAll method on the toString of that StringBuffer?
              Didn't come my reply #18 through? ;-)

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #22
                Originally posted by JosAH
                Didn't come my reply #18 through? ;-)

                kind regards,

                Jos
                I can see it here fine. The thread is becoming one of those. All the required ingredients have been posted here (more than once) so I'm out now.

                Comment

                • brendanmcdonagh
                  New Member
                  • Nov 2007
                  • 153

                  #23
                  sorry jos i know you are trying to help and have lost patience but #18 refers to replacing all charaters in stringbuffer with _. which i ve put on backburner for now.
                  My last question was more or less the same as the first in thios thread.

                  I know how to indexOf() for one character but the method i ve just pasted was a while loop to continue checking for multiple occurences b ut it's not doing it's job which as far as the api goes, it should.

                  Code:
                  private void setFeedback()
                      {
                  
                          int test = codeWord.indexOf(currentGuess);
                          
                          while(test != -1)
                          {
                              feedback.setCharAt(test, currentGuess);
                              test = codeWord.indexOf(test, currentGuess);
                              
                          }
                              
                           
                      }

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #24
                    Did you also overlook my reply #6?

                    kind regards,

                    Jos

                    Comment

                    • brendanmcdonagh
                      New Member
                      • Nov 2007
                      • 153

                      #25
                      I'm sorry for trying your patience today jos!

                      I got to the bottom of it in the end, it was just a case of having test and aChar the wrong way around.

                      Thank you so much for sticking in there!!

                      Brendan

                      Comment

                      Working...