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. Counting New Lines in a String

Counting New Lines in a String

Scheduled Pinned Locked Moved C#
csharp
9 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.
  • D Offline
    D Offline
    draco_iii
    wrote on last edited by
    #1

    I need to count the number of new lines(\n) in a string. I thought about using regular expressions, but I do not really understand them very well. .NET also does not provide a way to get this information. The only other way I can think of doing it is writing a function to count them through other string functions, if there is a faster or better way to do it I would really like to know.

    N L J 3 Replies Last reply
    0
    • D draco_iii

      I need to count the number of new lines(\n) in a string. I thought about using regular expressions, but I do not really understand them very well. .NET also does not provide a way to get this information. The only other way I can think of doing it is writing a function to count them through other string functions, if there is a faster or better way to do it I would really like to know.

      N Offline
      N Offline
      Nathan Blomquist
      wrote on last edited by
      #2

      Check out the StringReader class. You could do something like: public static int CountLinesInString(string str) { System.IO.StringReader strR = new System.IO.StringReader(str); string temp = strR.ReadLine(); int count = 0; while(temp != null) { count++; temp = strR.ReadLine(); } return count; } This would count the lines in the string. But this might not be exactly what you are looking for if you want an exact count of the character '\n'. Hope this helps, Nathan --------------------------- Hmmm... what's a signature?

      D 1 Reply Last reply
      0
      • N Nathan Blomquist

        Check out the StringReader class. You could do something like: public static int CountLinesInString(string str) { System.IO.StringReader strR = new System.IO.StringReader(str); string temp = strR.ReadLine(); int count = 0; while(temp != null) { count++; temp = strR.ReadLine(); } return count; } This would count the lines in the string. But this might not be exactly what you are looking for if you want an exact count of the character '\n'. Hope this helps, Nathan --------------------------- Hmmm... what's a signature?

        D Offline
        D Offline
        draco_iii
        wrote on last edited by
        #3

        Actually the StringReader class will work better for what I want to do. Thanks for pointing me to it.

        N 1 Reply Last reply
        0
        • D draco_iii

          Actually the StringReader class will work better for what I want to do. Thanks for pointing me to it.

          N Offline
          N Offline
          Nathan Blomquist
          wrote on last edited by
          #4

          No problem... I am glad I could help. :) --------------------------- Hmmm... what's a signature?

          1 Reply Last reply
          0
          • D draco_iii

            I need to count the number of new lines(\n) in a string. I thought about using regular expressions, but I do not really understand them very well. .NET also does not provide a way to get this information. The only other way I can think of doing it is writing a function to count them through other string functions, if there is a faster or better way to do it I would really like to know.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            str.Split('\n').Length (-1 : optional depends what u want) (and you have the lines too) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

            1 Reply Last reply
            0
            • D draco_iii

              I need to count the number of new lines(\n) in a string. I thought about using regular expressions, but I do not really understand them very well. .NET also does not provide a way to get this information. The only other way I can think of doing it is writing a function to count them through other string functions, if there is a faster or better way to do it I would really like to know.

              J Offline
              J Offline
              Julian Bucknall MSFT
              wrote on last edited by
              #6

              I'd say keep it simple and direct:

              public int CountNewLines(string s)
              {
              int result = 0;
              foreach (char c in s)
              {
              if (c == '\n') result++;
              }
              return result;
              }

              Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

              C 1 Reply Last reply
              0
              • J Julian Bucknall MSFT

                I'd say keep it simple and direct:

                public int CountNewLines(string s)
                {
                int result = 0;
                foreach (char c in s)
                {
                if (c == '\n') result++;
                }
                return result;
                }

                Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

                C Offline
                C Offline
                Csharp
                wrote on last edited by
                #7

                C#:


                private void button6_Click(object sender, System.EventArgs e)
                {
                int x="some\nstuff\nwith\nnewlines\nin".Split('\n').getupperbound(0); MessageBox.Show(x.ToString());
                }


                that gives you the amount of \n in the string :)

                G J 2 Replies Last reply
                0
                • C Csharp

                  C#:


                  private void button6_Click(object sender, System.EventArgs e)
                  {
                  int x="some\nstuff\nwith\nnewlines\nin".Split('\n').getupperbound(0); MessageBox.Show(x.ToString());
                  }


                  that gives you the amount of \n in the string :)

                  G Offline
                  G Offline
                  GISnet
                  wrote on last edited by
                  #8

                  Very nice :cool:

                  1 Reply Last reply
                  0
                  • C Csharp

                    C#:


                    private void button6_Click(object sender, System.EventArgs e)
                    {
                    int x="some\nstuff\nwith\nnewlines\nin".Split('\n').getupperbound(0); MessageBox.Show(x.ToString());
                    }


                    that gives you the amount of \n in the string :)

                    J Offline
                    J Offline
                    Julian Bucknall MSFT
                    wrote on last edited by
                    #9

                    Ahh, akin to using the SUV to get the mail ;) Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

                    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