hi, i have a question about string.

string a = "1 2 2 4 5";
            string b = "2 5";                      
          
            if (a.Substring(b)) // or need use contains?
            {
                Console.WriteLine("yes");
            }

            else
                Console.WriteLine("No");
        }

I want to check that if "2 5" is in a, then it will print "yes", i had tried Contains() before but it cannot, how should do?

The Contains() method of a string will return true only, if the given string is matched exactly to the source. So, you need to split the second string into individual characters and can use Contains() method of the source string like,

string a = "1 2 2 4 5";
            string b = "2 5";
            bool isExists = false;

            string[] temp = b.Split(' ');
            foreach (string s in temp)
            {
                if (a.Contains(s))
                    isExists = true;
                else
                {
                    isExists = false; 
                    break;
                }
            }
            Console.WriteLine(isExists? "Yes":"No");

The Contains() method of a string will return true only, if the given string is matched exactly to the source. So, you need to split the second string into individual characters and can use Contains() method of the source string like,

string a = "1 2 2 4 5";
            string b = "2 5";
            bool isExists = false;

            string[] temp = b.Split(' ');
            foreach (string s in temp)
            {
                if (a.Contains(s))
                    isExists = true;
                else
                {
                    isExists = false; 
                    break;
                }
            }
            Console.WriteLine(isExists? "Yes":"No");

but how to clear the array(temp) everytime it loop? because actually the string a is an array, it stilll have other words, and evrytime it loop i wants to clear the temp and store again..

Sorry, I didn't understand....could you please provide an example?

string[] ar = {"1 2 2 4 5","1 8 8 9","3 10 15"};
            string b = "2 5";
            bool isExists = false;

            foreach (var a in ar)
            {
                string[] temp = b.Split(' ');
                foreach (string s in temp)
                {
                    if (a.Contains(s))
                        isExists = true;
                    else
                    {
                        isExists = false;
                        break;
                    }
                }
                Console.WriteLine(isExists ? "Yes" : "No");
            }
[B]//This is what you mean?[/B]
Dictionary<string, double> sortList2 = new Dictionary<string, double>();
             string getText = textBox2.Text;
             string empty = "";
             Array arr;
             StringReader re1 = new StringReader(getText);
 foreach (string sortComb in listbox.Items)
             {
                 if (!(sortList2.ContainsKey(sortComb)))
                 {
                     sortList2.Add(sortComb, 0);
                 }                
                 
                 while ((empty = re1.ReadLine()) != null)
                 {
                     
                   arr= empty.Split(' ');                   

                     foreach(string k in arr)
                     {
                         if (empty.Contains(k))
                         {
                             double Count = (double)sortList2[sortComb];
                             sortList2[sortComb] = (Count + 1) /    textBox2.Lines.Length * 100;
                         }
                     }                                          
                 }
                                
                                
             }

This is my real program, there are so many lines of string in textbox2,i wants to know how the " 2 5" (for example) contains string in the textbox2

My problem now is i wants to clear the Array every time i loop ( arr.clear), but it seems like got error...

while ((empty = re1.ReadLine()) != null)
                 {
                  arr.clear();  // here, how to clear?
                   arr= empty.Split(' ');                   
 
                     foreach(string k in arr)
                     {
                         if (empty.Contains(k))
                         {
                             double Count = (double)sortList2[sortComb];
                             sortList2[sortComb] = (Count + 1) /    textBox2.Lines.Length * 100;
                         }
                     }                                          
                 }

Knvn's code contains an error that makes the result only tell you if the last item is in the list, not if all the items are in the list.

You don't need to clear the array, it's replaced each time through the loop (the old one is effectively deleted).

It's hard to tell what you want, but it sounds like you have two strings that contain items, and you want to know if every item in 'string1' is in 'string2'. If this is correct, could you point out which variables in your code would be 'string1' (The one with items to check) and which would be 'string2' (The list of items to check against)?

Dictionary<string, double> sortList2 = new Dictionary<string, double>();
             string getText = textBox2.Text;
             string empty = "";
             Array arr;
             StringReader re1 = new StringReader(getText);
             foreach (string sortComb in listbox.Items)
             {
                 if (!(sortList2.ContainsKey(sortComb)))
                 {
                     sortList2.Add(sortComb, 0);
                 }                
                 
                 while ((empty = re1.ReadLine()) != null)
                 {
                   [B]
                       //Lets consider the string 'empty' contains '1 2 2 4 5'
                   [/B]
                   arr= empty.Split(' ');
                    [B]
                       //After split 'arr' becomes:
                       //arr[0]=1; arr[1]=2; arr[2]=2; arr[3]=4; arr[5]=5
//------------------------------------------------------------------------------------------//
                    //In below loop, for each iteration k contains the successive value of 'arr'
                     [/B]
                     foreach(string k in arr) 
                     {
                       [B] //So, below condition is always ]True 
                        //Because, k is the part of the string 'empty' [/B]
                         if (empty.Contains(k))
                         {
                             double Count = (double)sortList2[sortComb];
                             sortList2[sortComb] = (Count + 1) /    textBox2.Lines.Length * 100;
                         }
                     }                                          
                 }                     
             }

What you are trying to achieve here? (What is the expected O/P?)

To Momerath:
Ya, u get my point, below is explaination about my codes/problems:

line 6 :read the 'sring1' from textbox
line 7 :get 'string2' from listbox
line 17:split 'string1' to Array
line 21 to 25: how to use string.Contains to check both the splited values in 'string1' ?

String1 (textBox2)
1 2 3 4
2 3 4 5

String2 (listbox)
1 2
1 4
1 5
2 4
2 5
1 2 4
1 2 5

//string2 is want to check each line of string1

arr.clear(); // here, how to clear?
arr= empty.Split(' ');

Declare arr as local to the loop.
Array arr= empty.Split(' ');

To Knvn:

empty="1 2 2 4 5"; //example

empty is the string to be checked, i have another string (sortComb) which used to check whether the sortcomb is contained in empty or not..
if sortcomb is contain in empty, then it will be:

double Count = (double)sortList2[sortComb];
                             sortList2[sortComb] = (Count + 1) /    textBox2.Lines.Length * 100;

sortcomb is my string2 (pattern as example above)

To Momerath:
Ya, you get my point, following is my explaination of my codes:

line6: read string1 from textBox2
line7: get string2 from listbox
line 17: split string2 to Array
line 19 to 24: how to do if string2 is contains in string1, then

double Count = (double)sortList2[sortComb];
                             sortList2[sortComb] = (Count + 1) /    textBox2.Lines.Length * 100;

String1 (empty)
1 2 3 4
2 3 4 5

String2 (sortComb)
1 2
1 3
1 4
1 5
2 3
2 3 4
2 3 5

OK now I understood. Check the updated code sample:

string[] firstString = { "1 2 3 4", "2 3 4 5" };
            string[] secondString = 
            {
                "1 2"
                ,"1 4"
                ,"1 5"
                ,"2 4"
                ,"2 5"
                ,"1 2 4"
                ,"1 2 5"
            };
            bool isExists = false;
            int count=0;

            foreach (var a in firstString)
            {
                foreach (var b in secondString)
                {
                    string[] temp = b.Split(' ');
                    foreach (string s in temp)
                    {
                        if (a.Contains(s))
                            isExists = true;
                        else
                        {
                            isExists = false;
                            break;
                        }
                    }
                    Console.WriteLine("firstString[" + count + "]: " + (isExists ? "Yes" : "No"));
                }
                Console.WriteLine();
                count++;
            }

Knvn's still contains a subtle bug. We fix it by moving the isExists to inside the loop and change the starting condition. We can also get rid of an 'else'.

foreach (var b in secondString {
    bool isExists = true; // note the different starting condition
    string[] temp = b.Split(' ');
    foreach (string s in temp) {
        if (a.Contains(s) == false) {
            isExists = false;
            break;
        }
    }
}

almost close to what i wants, just i dun wan to show "yes" or "no", in my codes, i have a sortList2.Add(sortComb, 0), if secondString ("1 2") is contain in the firstString("1 2 3 4"), then sortList2 will become sortList2(sortComb, 1)

double Count = (double)sortList2[sortComb];
                             sortList2[sortComb] = (Count + 1) /    textBox2.Lines.Length * 100;

In the codes as Knvn provided, line 20 just can compare 1 by 1 ("1" only and not "1 2"), got ways? to do that?

line 20 just can compare 1 by 1 ("1" only and not "1 2"), got ways? to do that?

1 and 2 are not a different numbers? you mean to compare the sequence?
if the is the case, then there is no need of split at all!!! you can directly write

foreach (var a in firstString)
            {
                foreach (var b in secondString)
                {
                    if (a.Contains(b))
                    {
                        //Do something
                    }
                }
            }

Ok , i will try do myself, Thanks all for the help

Knvn's still contains a subtle bug. We fix it by moving the isExists to inside the loop and change the starting condition. We can also get rid of an 'else'.

This is not a bug, but I agree that the code part you have given is more efficient than the one, which I wrote.

It is a bug, if the matching set is empty, it should match anything and your code will say that it doesn't. Not a major issue, but still a bug :)

hi, i had tried some codes as below:

string[] firstString = { "1 2 3 4", "2 3 4 5" };
string[] secondString =
{
"1 2"
,"1 4"
,"1 5"
,"2 4"
,"2 5"
,"1 2 4"
,"1 2 5"
};

foreach (string k in listbox.Items)
             {
                 if (!(sortList2.ContainsKey(k)))
                 {
                     sortList2.Add(kk, 0);
                 }                
             }     
             
             foreach (var a in secondString)
             {                                
                 arr = a.Split(' ');
               
                 foreach (var b in firstString)
                 {
                     bool isExists = true;
                   
                     foreach (string s in arr)
                     {
                         if (b.Contains(s))
                         {                           
                             continue;
                         }
                         else
                         {
                             isExists = false;
                             break;
                         }                                                                  
                     }

                     if (isExists = true)
                     {
                         double Count = (double)sortList2[a];
                         sortList2[a] = Count + 1;
                     }
                     else
                         break;
                 }               
             }

*firstString and secondString elements i using the example by Knvn, my program actual elements also look like him one.

Based on my code, after i do the calculation, all the outputs just only 2, example:
1 2 2
1 4 2
1 5 2
2 4 2
2 5 2
1 2 4 2
1 2 5 2

why would like that??

NM

what is NM?

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.