• 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 6494
441
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.
 
Master Rancher
Posts: 1061
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
Master Rancher
Posts: 1061
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: 10731
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: 62819
203
  • 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: 12963
217
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"...
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!