469,306 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

String .contains()

153 100+
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
Oct 21 '08 #1
24 9508
JosAH
11,448 Expert 8TB
Yep, you can also use a String as an argument for the indexOf() method, e.g.
"foobar".indexOf("oba") == 2

kind regards,

Jos
Oct 21 '08 #2
brendanmcdonagh
153 100+
cheers Jos,

I'm starting to think for myself!!
Oct 21 '08 #3
brendanmcdonagh
153 100+
I'm not so smart after all....

aString = "three"
aChar = 'e'

What would be the return of aString.indexOf(aChar) because of multiple occurences?
Oct 21 '08 #4
brendanmcdonagh
153 100+
Expand|Select|Wrap|Line Numbers
  1.     private void setStringBuffer()
  2.     {
  3.         int test = aString.indexOf(aChar);
  4.         while(test != -1)
  5.         {
  6.             aStringBuffer.setCharAt(test, aChar);
  7.             test = aString.indexOf(test + 1, aChar);
  8.         }
  9.  
  10.  
  11.     }
  12.  
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??
Oct 21 '08 #5
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1.     private void setStringBuffer()
  2.     {
  3.         int test = aString.indexOf(aChar);
  4.         while(test != -1)
  5.         {
  6.             aStringBuffer.setCharAt(test, aChar);
  7.             test = aString.indexOf(test + 1, aChar);
  8.         }
  9.  
  10.  
  11.     }
  12.  
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:

Expand|Select|Wrap|Line Numbers
  1. int nofCharInString(char aChar, String aString) { // assume String non-null
  2.    int count= 0;
  3.    for (int i= -1; (i= aString.indexOf(aChar, i+1)) >= 0; count++);
  4.    return count;
  5. }
  6.  
kind regards,

Jos
Oct 21 '08 #6
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1.     private void setStringBuffer()
  2.     {
  3.         int test = aString.indexOf(aChar);
  4.         while(test != -1)
  5.         {
  6.             aStringBuffer.setCharAt(test, aChar);
  7.             test = aString.indexOf(test + 1, aChar);
  8.         }
  9.  
  10.  
  11.     }
  12.  
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.
Oct 21 '08 #7
brendanmcdonagh
153 100+
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.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.StringBuffer.setCharAt(StringBuffer.java :211)
at tma01q3.Coder.setFeedback(Coder.java:64)
at tma01q3.Coder.updateState(Coder.java:79)
at tma01q3.Coder.turn(Coder.java:104)
at tma01q3.TestGame.main(TestGame.java:38)

which i think has something to do with this method

Expand|Select|Wrap|Line Numbers
  1.  
  2.     private void setFeedback()
  3.     {
  4.         int test = codeWord.indexOf(currentGuess);
  5.         System.out.println(test);
  6.         while(test != -1)
  7.         {
  8.             feedback.setCharAt(test, currentGuess);
  9.             test = codeWord.indexOf(test + 1, currentGuess);
  10.  
  11.         }
  12.  
  13.  
  14.     }
  15.  
  16.  
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(currentGuess) and if so setChar() of StringBuffer(feedback)
Oct 21 '08 #8
r035198x
13,262 8TB
From the API specs, setCharAt
Throws:
IndexOutOfBoundsException - if index is negative or greater than or equal to length().
Oct 21 '08 #9
JosAH
11,448 Expert 8TB
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.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.StringBuffer.setCharAt(StringBuffer.java :211)
at tma01q3.Coder.setFeedback(Coder.java:64)
at tma01q3.Coder.updateState(Coder.java:79)
at tma01q3.Coder.turn(Coder.java:104)
at tma01q3.TestGame.main(TestGame.java:38)

which i think has something to do with this method

Expand|Select|Wrap|Line Numbers
  1.  
  2.     private void setFeedback()
  3.     {
  4.         int test = codeWord.indexOf(currentGuess);
  5.         System.out.println(test);
  6.         while(test != -1)
  7.         {
  8.             feedback.setCharAt(test, currentGuess);
  9.             test = codeWord.indexOf(test + 1, currentGuess);
  10.  
  11.         }
  12.  
  13.  
  14.     }
  15.  
  16.  
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(currentGuess) and if so setChar() of StringBuffer(feedback)
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
Oct 21 '08 #10
brendanmcdonagh
153 100+
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(code.length());

thanks for keeping paience
Oct 21 '08 #11
r035198x
13,262 8TB
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(code.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.
Oct 21 '08 #12
brendanmcdonagh
153 100+
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:(
Oct 21 '08 #13
brendanmcdonagh
153 100+
There is a setLength() you guys are the best!
Oct 21 '08 #14
r035198x
13,262 8TB
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.
Oct 21 '08 #15
r035198x
13,262 8TB
After initializing with the String itself, you can then use replaceAll to change every character to underscore.
Oct 21 '08 #16
brendanmcdonagh
153 100+
There isn't a replaceAll method for StringBuffer??
Oct 21 '08 #17
JosAH
11,448 Expert 8TB
... or use an ordinary regular expression for that purpose:

Expand|Select|Wrap|Line Numbers
  1. String replaceNonChar(String s, char aChar) {
  2.    return s.replaceAll("[^"+aChar+"]", "_");
  3. }
  4.  
kind regards,

Jos
Oct 21 '08 #18
brendanmcdonagh
153 100+
ok im back to the reason i started this thread as my method isn't doing the job,

Expand|Select|Wrap|Line Numbers
  1. private void setFeedback()
  2.     {
  3.  
  4.         int test = codeWord.indexOf(currentGuess);
  5.  
  6.         while(test != -1)
  7.         {
  8.             feedback.setCharAt(test, currentGuess);
  9.             test = codeWord.indexOf(test, currentGuess);
  10.  
  11.         }
  12.  
  13.  
  14.     }
  15.  
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!
Oct 21 '08 #19
r035198x
13,262 8TB
Why not just use the replaceAll method on the toString of that StringBuffer?
Oct 21 '08 #20
JosAH
11,448 Expert 8TB
Why not just use the replaceAll method on the toString of that StringBuffer?
Didn't come my reply #18 through? ;-)

kind regards,

Jos
Oct 21 '08 #21
r035198x
13,262 8TB
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.
Oct 21 '08 #22
brendanmcdonagh
153 100+
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.

Expand|Select|Wrap|Line Numbers
  1. private void setFeedback()
  2.     {
  3.  
  4.         int test = codeWord.indexOf(currentGuess);
  5.  
  6.         while(test != -1)
  7.         {
  8.             feedback.setCharAt(test, currentGuess);
  9.             test = codeWord.indexOf(test, currentGuess);
  10.  
  11.         }
  12.  
  13.  
  14.     }
  15.  
Oct 21 '08 #23
JosAH
11,448 Expert 8TB
Did you also overlook my reply #6?

kind regards,

Jos
Oct 21 '08 #24
brendanmcdonagh
153 100+
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
Oct 21 '08 #25

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by nishit.gupta | last post: by
reply views Thread by Borse, Ganesh | last post: by
2 posts views Thread by j1mb0jay | last post: by
4 posts views Thread by Jeff | last post: by
1 post views Thread by Jon Skeet [C# MVP] | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.