Click to See Complete Forum and Search --> : strReverse in C#
dynamic_sysop
May 11th, 2004, 04:09 PM
no need for a demo project , just copy the function and paste to your application to use.
this carries out the same function as strReverse does in VB.
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(StringReverse("test 123"));
}
private string StringReverse(string ToReverse)
{
Array arr = ToReverse.ToCharArray();
Array.Reverse( arr ); // reverse the string
char[] c = (char[])arr;
byte[] b = System.Text.Encoding.Default.GetBytes(c);
return System.Text.Encoding.Default.GetString(b);
}
brown monkey
Jul 19th, 2004, 02:49 AM
i don't know much mate but i've got this
public string strReverse(string s)
{
int j=0;
char[] c=new char[s.Length];
for(int i=s.Length-1;i>=0;i--) c[j++]=s[i];
return new string(c);
}
hehe. not efficient enough compare to that of your code mate. and oh. sorry for bothers...
hellswraith
Oct 1st, 2004, 11:56 PM
I know it is frowned apon, but if you need to make use of it, why not just reference the Microsoft Visual Basic Runtime, then you can use this code:
MessageBox.Show(Microsoft.VisualBasic.Strings.StrReverse(textBox1.Text));
ricka0
Apr 18th, 2005, 11:23 PM
The classic interview question is "How do you reverse all the strings but preserve their order, ie, first is last and last is first"?
In the example, "test 123" become "123 test" or
"test 123 ricka" become "ricka 123 test"
string reverseEachString(string s){
System.Text.StringBuilder sb = new System.Text.StringBuilder(); // need to add mscorlib.dll
char[] seps = { ' ',',', '\n','\r' };
string [] splitAr = s.Split(seps);
for(int i=0;i<splitAr.Length;i++){
sb.Append(StringReverse(splitAr[i]));
sb.Append(" ");
}
return sb.ToString();
}
private void button1_Click(object sender, System.EventArgs e) {
string sr = "test 123 ricka";
MessageBox.Show("Reverse each char should yield \n \"akcir 321 tset\"","result = \"" + StringReverse(sr) + "\"");
MessageBox.Show("Double Reverse should yield original string \n \"" + sr + "\"",
"result = \"" + StringReverse(StringReverse(sr))+ "\"");
MessageBox.Show("should reverse order of strings \n & preserve each string \n original str = \"" + sr + "\"",
"result = \"" + reverseEachString(StringReverse(sr))+ "\"");
}
DNA7433
Jul 13th, 2005, 02:28 AM
Yeah, but do you know the trick for reversing the string without using extra memory buffers?
Sir Loin
Feb 21st, 2006, 09:18 AM
Wouldn't just cycling through a string suffice? I'm new to C#, and I've got this code.
private string strReverse(string Expression)
{
string result = null;
for (int i = 1; i <= Expression.Length; i++)
{
result += Expression.Substring(Expression.Length - i, 1);
}
return result;
}
It works, but is this a bad programming practice or something? It seems too simple for it to be good.
Thanks,
Sir Loin
DNA7433
Feb 23rd, 2006, 04:53 PM
Since strings are immutable, Sir Loin, inside your for loop a new string object is created and assigned back into result every time that line executes. Using a StringBuilder will be much faster.
Sir Loin
Feb 23rd, 2006, 06:12 PM
You mean like this?
private string strReverse(string Expression)
{
StringBuilder result = null;
for (int i = 1; i <= Expression.Length; i++)
{
result.Append(Expression.Substring(Expression.Length - i, 1));
}
return result.ToString();
}
What is the StringBuilder class, what are its pro's and con's?
Thanks,
Sir Loin
DNA7433
Feb 23rd, 2006, 07:34 PM
private string StrReverse(string expression)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= expression.Length; i++)
{
result.Append(expression[Expression.Length - i]);
}
return result.ToString();
}
You need to create a new instance of StringBuilder to use it. Also, Substring will return a new string object and since all you need is a single character you can simply use indexing.
THEROB
Feb 9th, 2007, 04:58 AM
no need for a demo project , just copy the function and paste to your application to use.
this carries out the same function as strReverse does in VB.
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(StringReverse("test 123"));
}
private string StringReverse(string ToReverse)
{
Array arr = ToReverse.ToCharArray();
Array.Reverse( arr ); // reverse the string
char[] c = (char[])arr;
byte[] b = System.Text.Encoding.Default.GetBytes(c);
return System.Text.Encoding.Default.GetString(b);
}
This speeds the code up:
private string StringReverse(string ToReverse)
{
Array arr = ToReverse.ToCharArray();
Array.Reverse(arr); // reverse the string
char[] c = (char[])arr;
return (new string(c));
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.