473,382 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 10011
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

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

Similar topics

5
by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to...
9
by: booksnore | last post by:
I am writing some code to search for strings that contain every letter of the alphabet. At the moment I am using the method below to check to see if a string contains every letter of the alphabet....
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
14
by: nishit.gupta | last post by:
Is their any single fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" , It can also...
0
by: Borse, Ganesh | last post by:
Hi, Am new to python. May someone please help me know this? How can we check whether one big string contains another small string? E.g. bigstring="python anaconda boa cobra" smallone="boa"
2
by: j1mb0jay | last post by:
Is there a function in PHP that will return a boolean if a String contains another given String. As what I am trying to do is disallow input that contains ../ or something else that would allow...
7
by: Karch | last post by:
I need to find the fastest way in terms of storage and searching to determine if a given string contains one of a member of a list of strings. So, think of it in terms of this: I have a string such...
4
by: Jeff | last post by:
Hey ..NET 3.5 I'm trying to search a string to determine if the string contains </table>, but string.Contains don't find it. I've used WebRequest/WebReponse to retrieve the html from a...
1
by: Jon Skeet [C# MVP] | last post by:
On Apr 30, 3:56 pm, Raja <rajesh.mad...@gmail.comwrote: Well, in fact it's *not* working fine - it's not behaving the same on SQL server as it would be in normal code. String.Contains is case-...
8
by: SMJT | last post by:
Does anyone know why the string contains function always returns true if the token is an empty string? I expected it to return false. "AnyOldText".Contains("") or...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.