Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
how should i check whether part of one string is present in another string,
my sample code is here

C#
string str1 = "This is first test string";
string str2 = "first test for me"

if(/* i want logic to check whether str2 contains first test in it*/)
{
........
}
Posted
Updated 14-Feb-12 21:07pm
v2
Comments
StM0n 15-Feb-12 2:59am    
Do you want to check the whole second string or only parts of it?
Sumit Memane 15-Feb-12 3:03am    
part of it
Code 89 15-Feb-12 3:30am    
string str1 = "This is first test string";
string str2 = "first test for me"

//check if str2 exists in str1
if (str1 .IndexOf(str2) > -1)
{
//str2 is within str1
}

Look at the string.Contains method[^]
Try:
C#
if (str2.Contains(str1))
   {
   ...
   }
 
Share this answer
 
Comments
Sumit Memane 15-Feb-12 3:03am    
Contains method work only if str1 as it is present in str2
if str2 contains other thing than str1 den it returns false
OriginalGriff 15-Feb-12 3:10am    
So what are you looking for? The longest common substring?
StM0n 15-Feb-12 3:05am    
I'm not quite sure, if he ment that... but we should let him talk :)
Sumit Memane 15-Feb-12 3:08am    
i tried for contains method
it does not work for my problem
If I understand your requirement correctly, you have to split string1 into separate words. String.Split()[^] can help you here.

Then, you have to concatenate every word with its successor to get several sequences of two subsequent words from string2. In this case, I would choose a simple + in favor of a StringBuilder[^].

Then you can use String.Contains()[^] with all those two-word-sequences to get their occurences within string2.
 
Share this answer
 
You can try the following solution
C#
void Main()
{

//This routine tests whether str2 or part of it is present in str1
//eg. first, first test, first test for etc.
string str1 = "This is first test string";
string str2 = "first test   for me";
string stringToFind=str2;
int spaceIndex;
    while (true){
        //Check whether str1 contains stringToFind
        if (str1.Contains(stringToFind)){
            Console.WriteLine("Str1 contains: " + stringToFind);
            break;
        }
        //Find the last position of space
        spaceIndex=stringToFind.LastIndexOf(' ');
        //If no space is left in the stringToFind, then we have searched
        //the first word already.  Hence, no more words to search. So quick loop
        if(spaceIndex==-1)
            break;
        //Trim is required to remove the trailing spaces, as the last index of
        //finds the last space.
        stringToFind = stringToFind.Substring(0,spaceIndex).Trim();
    }
}

If your problem is solved you may accept the solution, otherwise please post your queries.

PES
 
Share this answer
 
Try Googling "Longest common subsequence" ;)
 
Share this answer
 
Maybe this could help...

C#
public static Boolean isContainingParts(String Container_, String Parts_) {
    Boolean found = false;
    
    String[] parts = Parts_.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    IEnumerator enumerator = parts.GetEnumerator();
    
    while(!found && enumerator.MoveNext()) {
        found = Container_.Contains(enumerator.Current.ToString());
    }
    
    return found;
    
}


[edit]ooops... should've pressed refresh before posting :)[/edit]
 
Share this answer
 
v2

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