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. Like foreach but from end to begining ?

Like foreach but from end to begining ?

Scheduled Pinned Locked Moved C#
tutorialquestion
9 Posts 4 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.
  • E Offline
    E Offline
    ektoras
    wrote on last edited by
    #1

    string st="a very long string"; foreach(char ch in st) ..... How to make this from end to begining rapidly?

    H D 2 Replies Last reply
    0
    • E ektoras

      string st="a very long string"; foreach(char ch in st) ..... How to make this from end to begining rapidly?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      foreach in C# compiles to use the IEnumerator for a class that implements IEnumerable, such as a String. Unless the enumerator goes backward while you call MoveNext (the foreach does this automatically) you'll have to use a simple for loop:

      for (int i = st.Length - 1; i >= 0; i--)
      {
      char ch = st[i];
      // ...
      }

      This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

      E 1 Reply Last reply
      0
      • H Heath Stewart

        foreach in C# compiles to use the IEnumerator for a class that implements IEnumerable, such as a String. Unless the enumerator goes backward while you call MoveNext (the foreach does this automatically) you'll have to use a simple for loop:

        for (int i = st.Length - 1; i >= 0; i--)
        {
        char ch = st[i];
        // ...
        }

        This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

        E Offline
        E Offline
        ektoras
        wrote on last edited by
        #3

        I think this is very slow for a long string. I was thinking something like: string st = "a very long string" ; System.Text.StringBuilder sb = new System.Text.StringBuilder(st); System.Text.StringBuilder sb2 = new System.Text.StringBuilder(""); foreach(char ch in sb.ToString()) sb2.Insert(0,ch); foreach(char ch in sb2.ToString()) { ///////// } but I'm looking for something more faster.

        H R 2 Replies Last reply
        0
        • E ektoras

          I think this is very slow for a long string. I was thinking something like: string st = "a very long string" ; System.Text.StringBuilder sb = new System.Text.StringBuilder(st); System.Text.StringBuilder sb2 = new System.Text.StringBuilder(""); foreach(char ch in sb.ToString()) sb2.Insert(0,ch); foreach(char ch in sb2.ToString()) { ///////// } but I'm looking for something more faster.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Enumerators in the BCL do not go backward. Not every one goes "forward", either, in cases where it doesn't make sense like with a Hashtable that isn't a linear list of items. There really is no other way to do what you want short of reversing the string, but then you're taking an O(2n) performance hit rather than a O(n) hit. Even enumeration is an O(n) performance hit but the enumeration code is a litte faster. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

          1 Reply Last reply
          0
          • E ektoras

            string st="a very long string"; foreach(char ch in st) ..... How to make this from end to begining rapidly?

            D Offline
            D Offline
            Dominic Farr
            wrote on last edited by
            #5

            char[] chars = "this is a very long string, not!".ToCharArray(); Array.Reverse(chars); foreach(char c in chars) { // do what you want }

            E 1 Reply Last reply
            0
            • E ektoras

              I think this is very slow for a long string. I was thinking something like: string st = "a very long string" ; System.Text.StringBuilder sb = new System.Text.StringBuilder(st); System.Text.StringBuilder sb2 = new System.Text.StringBuilder(""); foreach(char ch in sb.ToString()) sb2.Insert(0,ch); foreach(char ch in sb2.ToString()) { ///////// } but I'm looking for something more faster.

              R Offline
              R Offline
              Rei Miyasaka
              wrote on last edited by
              #6

              Foreach itself is actually slower than for( ;; ) (for now, on .NET 1.1), so for a tight loop like this one, you'll want to use for( ;; ) anyway. And don't worry, for( ;; ) is a lot faster than you might think at first glance.

              E 1 Reply Last reply
              0
              • R Rei Miyasaka

                Foreach itself is actually slower than for( ;; ) (for now, on .NET 1.1), so for a tight loop like this one, you'll want to use for( ;; ) anyway. And don't worry, for( ;; ) is a lot faster than you might think at first glance.

                E Offline
                E Offline
                ektoras
                wrote on last edited by
                #7

                for( ; ; ) by itself is very fast. But to access elements in an array using indexes is slow.

                R 1 Reply Last reply
                0
                • D Dominic Farr

                  char[] chars = "this is a very long string, not!".ToCharArray(); Array.Reverse(chars); foreach(char c in chars) { // do what you want }

                  E Offline
                  E Offline
                  ektoras
                  wrote on last edited by
                  #8

                  Thank you Dominic Farr .

                  1 Reply Last reply
                  0
                  • E ektoras

                    for( ; ; ) by itself is very fast. But to access elements in an array using indexes is slow.

                    R Offline
                    R Offline
                    Rei Miyasaka
                    wrote on last edited by
                    #9

                    Take a look at this article: http://www.codeproject.com/dotnet/arrays.asp[^] Check out the section "Range Check Elimination: Use for(int i=0; i

                    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