problem in strings

csharp Malawi
  • 13 years ago

    Hiii everyone

        I had a problem in comparing two strings. my problem is i have to compare a string in a textbox with the text in the richtextbox, and if that strings occurs as many times then the count has to increase. The code i wrote for this is like this:

    string[ ] str1= {rt1.Text .ToString ()};

    string str2= txtbx.Text .ToString ();

    int icount=0;for (int i=0;i<=str1.Length -1;i++)

    {

    if(str2.Equals (str1[i]))

    {

    icount++;

    }

    }

    label1.Text =icount.ToString ();

     but the count is not increasing, i dont knw where the problem is? can any1 help me pls.

    thanks in advance

     

  • 13 years ago

    Hai priya,

            Use this code,i think this will solve your problem

    Yes

    bye........... 

     

  • 13 years ago

    Hai priya,

            Use this code,i think this will solve your problem

    Yes

    string str1 = richTextBox1.Text.ToString() ;

    string str2 = textBox1.Text.ToString() ;

    int icount = 0;

    for (int j = 0; j <= str2.Length - 1; j++)

    {

    for (int i = 0; i <= str1.Length - 1; i++)

    {

    if (string.Compare(str2.ToString().Substring(j,1),str1.ToString().Substring(i,1))==0)

    {

    icount++;

    }

    }

    }

    label1.Text = icount.ToString();

    bye........... 

     

  • 13 years ago

    hi priya , this source may be usefull for you, i have tested it

    string[] str1 = rt1.Lines;

    string str2 = txtbx.Text.ToString();

    int icount = 0;

    int s = str1.Length;

    for (int i = 0; i <= s - 1; i++) {

                                               if (str2.Equals(str1[i]))

                                                       { icount++;}

                                             }

    textBox1.Text = icount.ToString();

  • 13 years ago

    You could also try using regular expresions (System.Text.RegularBLOCKED EXPRESSION:

                Regex regex = new Regex(txtbx.Text);
                MatchCollection collection = regex.Matches(rt1.Text);
                int count = collection.Count;

    Joe
     

  • 13 years ago

    actually my problem is i have to split a text file by commas and store that it into different array. By using split am able to store entire thing in one array.My input text file is something is like this.........................

    Company,contact,age,sex

    oracle,,john,24,m

    IBM, Luci,22,f

    Microsoft,Mallesh,26,m

    Satyam,rao,25,m

    Ibm,rosy,22,f  this is how my input.txt file looks like.now i have to seperate each word by comma ans store all company names in a array, contact names in another so  on.................... The code i wrote for this is like this..

    FileStream fs;

    fs =
    new FileStream ( "input.txt", FileMode.Open);

    StreamReader sr= new StreamReader (fs );

    string str;while((str= sr.ReadLine ())!= null)

    {

    int i = str.IndexOf (",");

    char[] ch = {','};

    string[] comp = str.Split (ch,i);

    foreach(string s1 in comp)

    {

    MessageBox.Show (s1);

    }

    }

    pls any1 help me... I need it urgently....

  • 13 years ago

    I'm a little confused by what you want here, as you seem to have posted 2 different problems in the same posting??

    Your code looks to be ok, though I might be inclined to refine it a little: 

                using (FileStream fs = new FileStream("input.txt", FileMode.Open))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        char[] c = new char[] { ',' };                   

                        while (!sr.EndOfStream)
                        {
                            foreach (string s in sr.ReadLine().Split(c))
                            {
                                MessageBox.Show(s);
                            }
                        }
                    }
                }
     

     Joe
     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow