Hi all,
How can I compare between 2 strings , contain some words are same in each other and extracting the same word only ,,these 2 strings not contain spaces.
For example:
String1="catdogfish";
String2="dogfishmonky";
So, the result I want to get from compare them is "dogfish".
Is there any way to do this without use method equal because isn't work and 2 strings are different except some words....

Recommended Answers

All 5 Replies

Remember these are strings, not words. While I can guess what you mean, I have an edge here but your computer without a lot of work will not know that dogfishmonky when split up is dog fish monky or dogfish monky and so on.

Where is the spec for this app? Let's read it again.

You will have to use a loop that takes every word from the first string, and checks to see if that word is in the second string. The check is trivial, but without any spaces you are going to have fun trying to decide what is a "word" in the first string. Maybe you will have to try every substring of the first string, in which case you could start with the longest substring. It's not efficient, the run time is going to be O(n^2) or worse.

You are right, , so if the strings contain spaces ...I mean that str1="cat dog fish";
str2="dog fish monky";
How can substring every word and compare it with other string ?
Can I use the method split(" ");
str1.split (" ");
Then , how compare between them to get the equal word only...

Yes, split will do that nicely.
Once you have an array of words from string1 you can loop through that array seeing if each word is in the seocnd string. (Hint: have a look a String's contains method)

Yes ,, I do that and use contains method ,,
Thank you very much for your help....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.