• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Check if characters are in string  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Suppose,I have two string.
String A = "ABSD";
String B="KADBCSHSD";

Here, i want to check whether Character A,B,S and D are present in String B(order doesnt matter) or not.
How we can do using java program.

Thanks in advance...
 
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Sonu Dange
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String A ="I love my country";

I have to find "country" word is present in String A or not.

How we can do it using java program.

Thanks in advance.
 
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean you should return true if the word is in the sentence and false if it's not or just return the word?
For the first case you could use
for the second case put the first statement in an if and then call indexOf:

 
Daniel Demesmaecker
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sonu Dange wrote:
Suppose,I have two string.
String A = "ABSD";
String B="KADBCSHSD";

Here, i want to check whether Character A,B,S and D are present in String B(order doesnt matter) or not.
How we can do using java program.

Thanks in advance...


For this case your better to use a charArray:

So for every character in stringA it checks if stringB contains it, if so check is true, once it dosnt contains it a single time check is back to false and you break out of the loop
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:For this case your better to use a charArray

I agree, but putting it in a method would be even better, viz:
Winston
 
Marshal
Posts: 63787
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are not interested in order, nor whether there are any duplicates, consider creating a Set containing the letters of each piece of text. What will happen if you use this?
 
Sheriff
Posts: 13360
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... rolling your own method for this is unnecessary. See String indexOf(String str)

Edit: ok, I see now what OP said about "order does not matter"...
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!