-
May 11th, 2004, 04:09 PM
#1
Thread Starter
Frenzied Member
strReverse in C#
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.
VB Code:
[COLOR=Blue]private void[/COLOR] button1_Click([COLOR=Blue]object[/COLOR] sender, System.EventArgs e)
{
MessageBox.Show(StringReverse("test 123"));
}
[COLOR=Blue]private string[/COLOR] StringReverse([COLOR=Blue]string[/COLOR] ToReverse)
{
Array arr = ToReverse.ToCharArray();
Array.Reverse( arr ); [COLOR=Green]// reverse the string[/COLOR]
[COLOR=Blue]char[/COLOR][] c = ([COLOR=Blue]char[/COLOR][])arr;
[COLOR=Blue]byte[/COLOR][] b = System.Text.Encoding.Default.GetBytes(c);
[COLOR=Blue]return[/COLOR] System.Text.Encoding.Default.GetString(b);
}
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jul 19th, 2004, 02:49 AM
#2
Fanatic Member
i don't know much mate but i've got this
PHP Code:
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...
-
Oct 1st, 2004, 11:56 PM
#3
PowerPoster
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));
-
Apr 18th, 2005, 11:23 PM
#4
New Member
Re: strReverse in C#
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"
VB Code:
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))+ "\"");
}
-
Jul 13th, 2005, 02:28 AM
#5
Fanatic Member
Re: strReverse in C#
Yeah, but do you know the trick for reversing the string without using extra memory buffers?
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Feb 21st, 2006, 10:18 AM
#6
Fanatic Member
Re: strReverse in C#
Wouldn't just cycling through a string suffice? I'm new to C#, and I've got this code.
VB 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
-
Feb 23rd, 2006, 05:53 PM
#7
Fanatic Member
Re: strReverse in C#
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.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Feb 23rd, 2006, 07:12 PM
#8
Fanatic Member
Re: strReverse in C#
You mean like this?
Code:
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
-
Feb 23rd, 2006, 08:34 PM
#9
Fanatic Member
Re: strReverse in C#
Code:
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.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Feb 9th, 2007, 05:58 AM
#10
Fanatic Member
Re: strReverse in C#
Originally Posted by dynamic_sysop
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.
VB Code:
[COLOR=Blue]private void[/COLOR] button1_Click([COLOR=Blue]object[/COLOR] sender, System.EventArgs e)
{
MessageBox.Show(StringReverse("test 123"));
}
[COLOR=Blue]private string[/COLOR] StringReverse([COLOR=Blue]string[/COLOR] ToReverse)
{
Array arr = ToReverse.ToCharArray();
Array.Reverse( arr ); [COLOR=Green]// reverse the string[/COLOR]
[COLOR=Blue]char[/COLOR][] c = ([COLOR=Blue]char[/COLOR][])arr;
[COLOR=Blue]byte[/COLOR][] b = System.Text.Encoding.Default.GetBytes(c);
[COLOR=Blue]return[/COLOR] System.Text.Encoding.Default.GetString(b);
}
This speeds the code up:
Code:
private string StringReverse(string ToReverse)
{
Array arr = ToReverse.ToCharArray();
Array.Reverse(arr); // reverse the string
char[] c = (char[])arr;
return (new string(c));
}
My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|