Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i have two strings like below
var str1="Hyderabad,Chennai,Pune,Delhi";
var str2="Cybergateway,hitech city,Hyderabad,Telangana,India";

here i compared above string, if it is there any matching value returns as matched message.

now i am getting matched messge, here problem is i want to find matched value/word/slice and it stored into a variable from both strings.

Actually i want to get Hyderabad is out put. but i don't know how to get matched value in two strings.

can u help me.
Thanks in advance
Posted

You need to split[^] both strings to arrays:
Array1                Array2
0    Hyderabad        Cybergateway
1    Chennai          hitech city
2    Pune             Hyderabad
3    Delhi            Telangana
4                     India

Then you need to compare them[^] using 2 loops:
First loop         Second loop            Result
0    Hyderabad     0    Cybergateway      False
0    Hyderabad     1    hitech city       False
0    Hyderabad     2    Hyderabad         True
0    Hyderabad     3    Telangana         False
0    Hyderabad     4    India             False
1    Chennai       0    Cybergateway      False
1    Chennai       1    hitech city       False
1    Chennai       2    Hyderabad         False
1    Chennai       3    Telangana         False
1    Chennai       4    India             False
2 ....
//and so on...


If you're more familiar with javascript, you can use indexOf Method (Array) (JavaScript)[^].

For further information, please see:
Array Object (JavaScript)[^]
Using Arrays (JavaScript)[^]
 
Share this answer
 
v2
Comments
CPallini 23-Jul-14 12:08pm    
5.
Maciej Los 23-Jul-14 12:10pm    
Thank you, Carlo ;)
Unareshraju 24-Jul-14 0:59am    
Hi Maciej thanks for your replay, i did comparison. i need matched word from both. please read the question.
Maciej Los 24-Jul-14 2:10am    
Please, read my answer after update. I'm almost sure that now you'll be able to understand what i'm trying to tell you.
Check this Hashtable implementation How do I implement a Dictionary or Hashtable in Javascript?[^]

The pseudo algorithm could look like this

function IdentifyCommonString(string srcString1, string srcString2)
{
var hashTable = new HashTable();
var sourceArray1 = srcString1.split(",");
var sourceArray2 = srcString2.split(",");
var arrayLength = sourceArray1.length;
for (var i = 0; i < arrayLength; i++) {
var item = hashTable.get_item(sourceArray1[i]);
if (!item)
hashTable.set_item(item, item);
else {
//our common string has now been identified
alert(item);
}
}

arrayLength = sourceArray2.length;
for (var k = 0; i < arrayLength; k++) {
var item = hashTable.get_item(sourceArray2[i]);
if (!item)
hashTable.set_item(item, item);
else {
//our common string has now been identified
alert(item);
}
}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900