Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to count a string for \ character?

How to count a string for \ character?

Scheduled Pinned Locked Moved C#
c++helptutorialquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matjaz xyz
    wrote on last edited by
    #1

    Hi. I just cant get this to work right! :( I tried two ways... yeah... only one is correct... but the returned value is not correct. This is my function:

    public void DoYourThingBaby()
    {
    string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem;
    StreamReader sr = new StreamReader(pot);
    string tempF = sr.ReadToEnd();
    sr.Close();

            char\[\] tempC = tempF.ToCharArray();
    

    // this
    int stevc=0;
    foreach(char chr in tempC)
    if(chr=='\')
    stevc++;

    // or this
    label1.Text = Convert.ToString(tempF.IndexOf(@"\", 30));
    }

    the code counts something... but its not the right characters. i tried it with my file. if i count it in ultraedit - there are 18 characters '\'. with my application - around 300 :D help please :)? thanks in advance

    Regards, Matjaž

    R M 2 Replies Last reply
    0
    • M Matjaz xyz

      Hi. I just cant get this to work right! :( I tried two ways... yeah... only one is correct... but the returned value is not correct. This is my function:

      public void DoYourThingBaby()
      {
      string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem;
      StreamReader sr = new StreamReader(pot);
      string tempF = sr.ReadToEnd();
      sr.Close();

              char\[\] tempC = tempF.ToCharArray();
      

      // this
      int stevc=0;
      foreach(char chr in tempC)
      if(chr=='\')
      stevc++;

      // or this
      label1.Text = Convert.ToString(tempF.IndexOf(@"\", 30));
      }

      the code counts something... but its not the right characters. i tried it with my file. if i count it in ultraedit - there are 18 characters '\'. with my application - around 300 :D help please :)? thanks in advance

      Regards, Matjaž

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Wild guess - something to do with Unicode/CodePage? If you open the file up using the binary editor is every other character zero?

      Regards, Rob Philpott.

      1 Reply Last reply
      0
      • M Matjaz xyz

        Hi. I just cant get this to work right! :( I tried two ways... yeah... only one is correct... but the returned value is not correct. This is my function:

        public void DoYourThingBaby()
        {
        string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem;
        StreamReader sr = new StreamReader(pot);
        string tempF = sr.ReadToEnd();
        sr.Close();

                char\[\] tempC = tempF.ToCharArray();
        

        // this
        int stevc=0;
        foreach(char chr in tempC)
        if(chr=='\')
        stevc++;

        // or this
        label1.Text = Convert.ToString(tempF.IndexOf(@"\", 30));
        }

        the code counts something... but its not the right characters. i tried it with my file. if i count it in ultraedit - there are 18 characters '\'. with my application - around 300 :D help please :)? thanks in advance

        Regards, Matjaž

        M Offline
        M Offline
        Matjaz xyz
        wrote on last edited by
        #3

        Ok. Dont know why my solution didnt work... but i've modified it a bit - a bit of a hax :P If i putted '\\' char to search... it found something. What did it found - i dont know... but it wasn't right. So, i've tried this - created a new textbox and enter one character inside - the '\'. And with this IT WORKED. So, here is the new code, that works if anyone's intrested. PS: thanks for your reply Rob.

        public void DoYourThingBaby()
        {

                string pot = @Application.StartupPath + @"\\" + lstFILES.SelectedItem;
                StreamReader sr = new StreamReader(pot);
                string tempF = sr.ReadToEnd();
                sr.Close();
        
                char\[\] tempC = tempF.ToCharArray();
        
                //textBox1.Text = tempF;
        
                int stevc=0;
                foreach(char chr in tempC)
                    if(chr==Convert.ToChar(textBox1.Text))
                        stevc++;
        
                
            }
        

        Regards, Matjaž

        J 1 Reply Last reply
        0
        • M Matjaz xyz

          Ok. Dont know why my solution didnt work... but i've modified it a bit - a bit of a hax :P If i putted '\\' char to search... it found something. What did it found - i dont know... but it wasn't right. So, i've tried this - created a new textbox and enter one character inside - the '\'. And with this IT WORKED. So, here is the new code, that works if anyone's intrested. PS: thanks for your reply Rob.

          public void DoYourThingBaby()
          {

                  string pot = @Application.StartupPath + @"\\" + lstFILES.SelectedItem;
                  StreamReader sr = new StreamReader(pot);
                  string tempF = sr.ReadToEnd();
                  sr.Close();
          
                  char\[\] tempC = tempF.ToCharArray();
          
                  //textBox1.Text = tempF;
          
                  int stevc=0;
                  foreach(char chr in tempC)
                      if(chr==Convert.ToChar(textBox1.Text))
                          stevc++;
          
                  
              }
          

          Regards, Matjaž

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Can I ask what the point in the final 3 lines are below?

          Matjaž Grahek wrote:

          string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem; StreamReader sr = new StreamReader(pot); string tempF = sr.ReadToEnd(); sr.Close();

          Seems a bit of a WTF to me.

          M 1 Reply Last reply
          0
          • J J4amieC

            Can I ask what the point in the final 3 lines are below?

            Matjaž Grahek wrote:

            string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem; StreamReader sr = new StreamReader(pot); string tempF = sr.ReadToEnd(); sr.Close();

            Seems a bit of a WTF to me.

            M Offline
            M Offline
            Matjaz xyz
            wrote on last edited by
            #5

            Bah... it IS WTF :) but its easier for my understanding... dont even bother with this. Programming is cool because of this - you have soooooo many different ways to make a program work.

            Regards, Matjaž

            B 1 Reply Last reply
            0
            • M Matjaz xyz

              Bah... it IS WTF :) but its easier for my understanding... dont even bother with this. Programming is cool because of this - you have soooooo many different ways to make a program work.

              Regards, Matjaž

              B Offline
              B Offline
              Ben Fair
              wrote on last edited by
              #6

              Just Split() the string on the char you want to count, the count will be the number of resulting array elements minus 1 (have to escape the backslash as '\\'). Most likely your results were thrown off by the escaping of special characters in the text (CRLF becomes \r\n, TAB becomes \t, etc.) and perhaps it brought them over into the char[] in that manner. Either way, this will do the trick:

              public void DoYourThingBaby()
              {
              string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem;
              if(!System.IO.File.Exists(pot))
              return;
              int count = System.IO.File.ReadAllText(pot).Split('\\').Length - 1
              }

              Keep It Simple Stupid! (KISS)

              J 1 Reply Last reply
              0
              • B Ben Fair

                Just Split() the string on the char you want to count, the count will be the number of resulting array elements minus 1 (have to escape the backslash as '\\'). Most likely your results were thrown off by the escaping of special characters in the text (CRLF becomes \r\n, TAB becomes \t, etc.) and perhaps it brought them over into the char[] in that manner. Either way, this will do the trick:

                public void DoYourThingBaby()
                {
                string pot = @Application.StartupPath + @"\" + lstFILES.SelectedItem;
                if(!System.IO.File.Exists(pot))
                return;
                int count = System.IO.File.ReadAllText(pot).Split('\\').Length - 1
                }

                Keep It Simple Stupid! (KISS)

                J Offline
                J Offline
                Jon Rista
                wrote on last edited by
                #7

                You can use LINQ to accomplish the same thing in a more expressive manner:

                int bkslashCount = 0;
                string pot = Path.Combine(Application.StartupPath, lstFILES.SelectedItem); // USE Path.Combine!!
                if (File.Exists(pot))
                {
                string data = File.ReadAllText(pot);
                bkslashCount = data.ToCharArray().Where(c => c == '/').Count();
                }

                L 1 Reply Last reply
                0
                • J Jon Rista

                  You can use LINQ to accomplish the same thing in a more expressive manner:

                  int bkslashCount = 0;
                  string pot = Path.Combine(Application.StartupPath, lstFILES.SelectedItem); // USE Path.Combine!!
                  if (File.Exists(pot))
                  {
                  string data = File.ReadAllText(pot);
                  bkslashCount = data.ToCharArray().Where(c => c == '/').Count();
                  }

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Hi, probably a lot cheaper is count=str.Length-str.Replace("\\","").Length; :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Good riddance W


                  modified on Friday, June 10, 2011 12:07 PM

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups