Like foreach but from end to begining ?
-
string st="a very long string"; foreach(char ch in st) ..... How to make this from end to begining rapidly?
foreach
in C# compiles to use theIEnumerator
for a class that implementsIEnumerable
, such as aString
. Unless the enumerator goes backward while you callMoveNext
(theforeach
does this automatically) you'll have to use a simplefor
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]
-
foreach
in C# compiles to use theIEnumerator
for a class that implementsIEnumerable
, such as aString
. Unless the enumerator goes backward while you callMoveNext
(theforeach
does this automatically) you'll have to use a simplefor
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]
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. -
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.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] -
string st="a very long string"; foreach(char ch in st) ..... How to make this from end to begining rapidly?
char[] chars = "this is a very long string, not!".ToCharArray(); Array.Reverse(chars); foreach(char c in chars) { // do what you want }
-
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.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.
-
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.
-
char[] chars = "this is a very long string, not!".ToCharArray(); Array.Reverse(chars); foreach(char c in chars) { // do what you want }
-
for( ; ; ) by itself is very fast. But to access elements in an array using indexes is slow.
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