But I can't seem to find the answer.
The question is how do you reverse the words in a string?
Or how do you reverse the numbers listed in a string?
The example is usually something like:
Turn this string "1,2,3,4,..."
Into "...4,3,2,1"
This one seems hard enough let alone trying to turn a string of
space-seperated words around(is that even possible? a trick question
perhaps?)
Again, any help here much appreciated.
Thanks!
Jeff 14 2266
Hi,
Is this the kind of thing you are after?
using System;
namespace Reverse
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
string str = "1,2,3,4";
string result = "";
for (int i = str.Length - 1; i>=0; i--)
{
result += str.Substring(i, 1);
}
Console.WriteLine("Start string: " + str);
Console.WriteLine("Result string: " + result);
Console.ReadLine();
}
}
}
Hope that helps... Although there may be easier ways to do it...
John
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02... But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string?
The example is usually something like: Turn this string "1,2,3,4,..." Into "...4,3,2,1"
This one seems hard enough let alone trying to turn a string of space-seperated words around(is that even possible? a trick question perhaps?)
Again, any help here much appreciated. Thanks!
Jeff
One approach:
Use String.Split() with a comma as the delimiter, to break up the numeric
substrings into an array. Then iterate over the array from the end to the
beginning, to reassemble the substrings into the order you want. For
example:
HTH,
Tom Dacon
Dacon Software Consulting
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02... But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string?
The example is usually something like: Turn this string "1,2,3,4,..." Into "...4,3,2,1"
This one seems hard enough let alone trying to turn a string of space-seperated words around(is that even possible? a trick question perhaps?)
Again, any help here much appreciated. Thanks!
Jeff
One approach:
Use String.Split() with a comma as the delimiter, to break up the numeric
substrings into an array. Then iterate over the array from the end to the
beginning, to reassemble the substrings into the order you want. For
example:
string input = "1,2,3,4";
string output = "";
string[] substrings = input.Split(new char[] { ',' });
for ( int i = substrings.Length - 1; i >= 0; i-- )
{
if ( output != "" )
output += ",";
output += substrings[i];
}
Extra points if you use a StringBuilder instance to assemble the output.
HTH,
Tom Dacon
Dacon Software Consulting
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02... But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string?
The example is usually something like: Turn this string "1,2,3,4,..." Into "...4,3,2,1"
This one seems hard enough let alone trying to turn a string of space-seperated words around(is that even possible? a trick question perhaps?)
Again, any help here much appreciated. Thanks!
Jeff
Try this
//convert to character array
char [] myArray = myString.ToCharArray()
//reverse the arrary
Array.Reverse(myArray);
//store in new string
string newString = new string(myArray);
Shak.
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02... But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string?
The example is usually something like: Turn this string "1,2,3,4,..." Into "...4,3,2,1"
This one seems hard enough let alone trying to turn a string of space-seperated words around(is that even possible? a trick question perhaps?)
Again, any help here much appreciated. Thanks!
Jeff
Isn't the simple code always the most elegant? :-)
I mean who would have ever thought of checking the
documentation to determine which members of the
Array class are supported?
What will they think of next.
--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:ud**************@tk2msftngp13.phx.gbl... Try this
//convert to character array char [] myArray = myString.ToCharArray() //reverse the arrary Array.Reverse(myArray); //store in new string string newString = new string(myArray);
Shak.
"z_learning_tester" <so*****@microsoft.com> wrote in message news:ej2Bc.71373$eu.3683@attbi_s02... But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string?
The example is usually something like: Turn this string "1,2,3,4,..." Into "...4,3,2,1"
This one seems hard enough let alone trying to turn a string of space-seperated words around(is that even possible? a trick question perhaps?)
Again, any help here much appreciated. Thanks!
Jeff
clintonG <csgallagher@REMOVETHISTEXT> wrote: Isn't the simple code always the most elegant? :-)
I mean who would have ever thought of checking the documentation to determine which members of the Array class are supported?
What will they think of next.
On the other hand, that will reverse 12,34 to 43,21 which I suspect
isn't what's wanted.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
The Reverse method would provide acceptable results on the string
of characters that was given but I would concur, in most any other
form of delineated characters a split would be required.
But that doesn't change the circumstances for we neophytes as I have
found that a quick trip to my local copy of the MSDN Library or
Google before posting to newsgroups has proven to be insightful and
other neophytes should be encouraged to do the same even if it means
beating them over the head with a mouse. My own lumps and bruises
are almost gone :-)
--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... clintonG <csgallagher@REMOVETHISTEXT> wrote: Isn't the simple code always the most elegant? :-)
I mean who would have ever thought of checking the documentation to determine which members of the Array class are supported?
What will they think of next.
On the other hand, that will reverse 12,34 to 43,21 which I suspect isn't what's wanted.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Thanks for the help.
Honestly I would rather go here than MSDN which one can muddle through for
weeks looking for a simple answer. Sure if you need an extremely specific
answer and have 10 keywords it will find an example(great!) but for basic
questions its just lame and gives *too much* info IMO.
Actually I can honestly say that so far I have never gotten the answer I've
needed on anything whatsoever from MSDN. I always get lost in the ocean of
data. But then I'm a beginner. I'm sure there's tricks :-)
Thanks again,
Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message
news:ug**************@tk2msftngp13.phx.gbl... The Reverse method would provide acceptable results on the string of characters that was given but I would concur, in most any other form of delineated characters a split would be required.
But that doesn't change the circumstances for we neophytes as I have found that a quick trip to my local copy of the MSDN Library or Google before posting to newsgroups has proven to be insightful and other neophytes should be encouraged to do the same even if it means beating them over the head with a mouse. My own lumps and bruises are almost gone :-)
-- <%= Clinton Gallagher A/E/C Consulting, Web Design, e-Commerce Software Development Wauwatosa, Milwaukee County, Wisconsin USA NET csgallagher@ REMOVETHISTEXT metromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/ "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om... clintonG <csgallagher@REMOVETHISTEXT> wrote: Isn't the simple code always the most elegant? :-)
I mean who would have ever thought of checking the documentation to determine which members of the Array class are supported?
What will they think of next.
On the other hand, that will reverse 12,34 to 43,21 which I suspect isn't what's wanted.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Thanks all for the replies.
This variety of feedback is invaluable to a beginner like me :-)
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02... But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string?
The example is usually something like: Turn this string "1,2,3,4,..." Into "...4,3,2,1"
This one seems hard enough let alone trying to turn a string of space-seperated words around(is that even possible? a trick question perhaps?)
Again, any help here much appreciated. Thanks!
Jeff
Oh, I finally got your point...
Sorry, a little slow getting this stuff. C# makes Oracle look like a
cakewalk :-)
Anyhow, yeah this solution would work on my string but not on ints over 10.
Thanks for pointing that out!
Jeff
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... clintonG <csgallagher@REMOVETHISTEXT> wrote: Isn't the simple code always the most elegant? :-)
I mean who would have ever thought of checking the documentation to determine which members of the Array class are supported?
What will they think of next.
On the other hand, that will reverse 12,34 to 43,21 which I suspect isn't what's wanted.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Yep, that works nicely :-)
Thanks!
Jeff
"John Young" <polomint77ATAThotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Hi,
Is this the kind of thing you are after?
using System;
namespace Reverse { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { // // TODO: Add code to start application here // string str = "1,2,3,4"; string result = ""; for (int i = str.Length - 1; i>=0; i--) { result += str.Substring(i, 1); } Console.WriteLine("Start string: " + str); Console.WriteLine("Result string: " + result); Console.ReadLine(); } } }
Hope that helps... Although there may be easier ways to do it...
John
"z_learning_tester" <so*****@microsoft.com> wrote in message news:ej2Bc.71373$eu.3683@attbi_s02... But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string?
The example is usually something like: Turn this string "1,2,3,4,..." Into "...4,3,2,1"
This one seems hard enough let alone trying to turn a string of space-seperated words around(is that even possible? a trick question perhaps?)
Again, any help here much appreciated. Thanks!
Jeff
I understand and agree that the MSDN website is humongous
but you should try to get a copy of the MSDN Library which
can be installed locally and is much more user freindly as it
does not include everything Microsoft has ever developed as the
MSDN website seems to.
A copy of the MSDN Library is distributed with MSDN subscriptions
and you may know somebody that can give you a copy of just the
library or dig into Microsoft to learn of you can obtain just a copy of
the MSDN Library itself. If you have Visual Studio.NET or have seen
its help system you were looking at a near equivalent to the
MSDN Library as both are deployed using Microsoft's HTML
Help 2.0 which is not being distributed unless embedded into one
of their products or distributed as the help for one of their SDKs.
--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:IFRBc.88317$HG.66021@attbi_s53... Thanks for the help. Honestly I would rather go here than MSDN which one can muddle through for weeks looking for a simple answer. Sure if you need an extremely specific answer and have 10 keywords it will find an example(great!) but for basic questions its just lame and gives *too much* info IMO.
Actually I can honestly say that so far I have never gotten the answer I've needed on anything whatsoever from MSDN. I always get lost in the ocean of data. But then I'm a beginner. I'm sure there's tricks :-) Thanks again,
Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message news:ug**************@tk2msftngp13.phx.gbl... The Reverse method would provide acceptable results on the string of characters that was given but I would concur, in most any other form of delineated characters a split would be required.
But that doesn't change the circumstances for we neophytes as I have found that a quick trip to my local copy of the MSDN Library or Google before posting to newsgroups has proven to be insightful and other neophytes should be encouraged to do the same even if it means beating them over the head with a mouse. My own lumps and bruises are almost gone :-)
-- <%= Clinton Gallagher A/E/C Consulting, Web Design, e-Commerce Software Development Wauwatosa, Milwaukee County, Wisconsin USA NET csgallagher@ REMOVETHISTEXT metromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/ "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om... clintonG <csgallagher@REMOVETHISTEXT> wrote: > Isn't the simple code always the most elegant? :-) > > I mean who would have ever thought of checking the > documentation to determine which members of the > Array class are supported? > > What will they think of next.
On the other hand, that will reverse 12,34 to 43,21 which I suspect isn't what's wanted.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Actually I have VS but didn't install the MSDN help due to my experiences
getting lost in the web site.
I just installed it and you're right, the distributed version is far more
condensed and useful.
Thanks for the tip!
Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl... I understand and agree that the MSDN website is humongous but you should try to get a copy of the MSDN Library which can be installed locally and is much more user freindly as it does not include everything Microsoft has ever developed as the MSDN website seems to.
A copy of the MSDN Library is distributed with MSDN subscriptions and you may know somebody that can give you a copy of just the library or dig into Microsoft to learn of you can obtain just a copy of the MSDN Library itself. If you have Visual Studio.NET or have seen its help system you were looking at a near equivalent to the MSDN Library as both are deployed using Microsoft's HTML Help 2.0 which is not being distributed unless embedded into one of their products or distributed as the help for one of their SDKs.
-- <%= Clinton Gallagher A/E/C Consulting, Web Design, e-Commerce Software Development Wauwatosa, Milwaukee County, Wisconsin USA NET csgallagher@ REMOVETHISTEXT metromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/ "z_learning_tester" <so*****@microsoft.com> wrote in message news:IFRBc.88317$HG.66021@attbi_s53... Thanks for the help. Honestly I would rather go here than MSDN which one can muddle through
for weeks looking for a simple answer. Sure if you need an extremely
specific answer and have 10 keywords it will find an example(great!) but for
basic questions its just lame and gives *too much* info IMO.
Actually I can honestly say that so far I have never gotten the answer
I've needed on anything whatsoever from MSDN. I always get lost in the ocean
of data. But then I'm a beginner. I'm sure there's tricks :-) Thanks again,
Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in
message news:ug**************@tk2msftngp13.phx.gbl... The Reverse method would provide acceptable results on the string of characters that was given but I would concur, in most any other form of delineated characters a split would be required.
But that doesn't change the circumstances for we neophytes as I have found that a quick trip to my local copy of the MSDN Library or Google before posting to newsgroups has proven to be insightful and other neophytes should be encouraged to do the same even if it means beating them over the head with a mouse. My own lumps and bruises are almost gone :-)
-- <%= Clinton Gallagher A/E/C Consulting, Web Design, e-Commerce Software Development Wauwatosa, Milwaukee County, Wisconsin USA NET csgallagher@ REMOVETHISTEXT metromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/ "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om... > clintonG <csgallagher@REMOVETHISTEXT> wrote: > > Isn't the simple code always the most elegant? :-) > > > > I mean who would have ever thought of checking the > > documentation to determine which members of the > > Array class are supported? > > > > What will they think of next. > > On the other hand, that will reverse 12,34 to 43,21 which I suspect > isn't what's wanted. > > -- > Jon Skeet - <sk***@pobox.com> > http://www.pobox.com/~skeet > If replying to the group, please do not mail me too
So there you go now :-)
--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:JI0Cc.76665$Hg2.55932@attbi_s04... Actually I have VS but didn't install the MSDN help due to my experiences getting lost in the web site. I just installed it and you're right, the distributed version is far more condensed and useful. Thanks for the tip!
Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message news:eV**************@TK2MSFTNGP11.phx.gbl... I understand and agree that the MSDN website is humongous but you should try to get a copy of the MSDN Library which can be installed locally and is much more user freindly as it does not include everything Microsoft has ever developed as the MSDN website seems to.
A copy of the MSDN Library is distributed with MSDN subscriptions and you may know somebody that can give you a copy of just the library or dig into Microsoft to learn of you can obtain just a copy of the MSDN Library itself. If you have Visual Studio.NET or have seen its help system you were looking at a near equivalent to the MSDN Library as both are deployed using Microsoft's HTML Help 2.0 which is not being distributed unless embedded into one of their products or distributed as the help for one of their SDKs.
-- <%= Clinton Gallagher A/E/C Consulting, Web Design, e-Commerce Software Development Wauwatosa, Milwaukee County, Wisconsin USA NET csgallagher@ REMOVETHISTEXT metromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/ "z_learning_tester" <so*****@microsoft.com> wrote in message news:IFRBc.88317$HG.66021@attbi_s53... Thanks for the help. Honestly I would rather go here than MSDN which one can muddle through for weeks looking for a simple answer. Sure if you need an extremely specific answer and have 10 keywords it will find an example(great!) but for basic questions its just lame and gives *too much* info IMO.
Actually I can honestly say that so far I have never gotten the answer I've needed on anything whatsoever from MSDN. I always get lost in the ocean of data. But then I'm a beginner. I'm sure there's tricks :-) Thanks again,
Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message news:ug**************@tk2msftngp13.phx.gbl... > The Reverse method would provide acceptable results on the string > of characters that was given but I would concur, in most any other > form of delineated characters a split would be required. > > But that doesn't change the circumstances for we neophytes as I have > found that a quick trip to my local copy of the MSDN Library or > Google before posting to newsgroups has proven to be insightful and > other neophytes should be encouraged to do the same even if it means > beating them over the head with a mouse. My own lumps and bruises > are almost gone :-) > > -- > <%= Clinton Gallagher > A/E/C Consulting, Web Design, e-Commerce Software Development > Wauwatosa, Milwaukee County, Wisconsin USA > NET csgallagher@ REMOVETHISTEXT metromilwaukee.com > URL http://www.metromilwaukee.com/clintongallagher/ > > > > "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message > news:MP************************@msnews.microsoft.c om... > > clintonG <csgallagher@REMOVETHISTEXT> wrote: > > > Isn't the simple code always the most elegant? :-) > > > > > > I mean who would have ever thought of checking the > > > documentation to determine which members of the > > > Array class are supported? > > > > > > What will they think of next. > > > > On the other hand, that will reverse 12,34 to 43,21 which I suspect > > isn't what's wanted. > > > > -- > > Jon Skeet - <sk***@pobox.com> > > http://www.pobox.com/~skeet > > If replying to the group, please do not mail me too > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Vernon |
last post by:
Hi anyone,
This is my code.
int num
num = 0;
for(int a=0; a<count; ++a){
if(current->transID==aNo)
++num;
current = current->next;
|
by: Chefry |
last post by:
I'm trying to set up an off the shelf script and keep getting an error.
My host set up the mysql on my site and I changed the variables I had to in the
settings.php file but I keep getting the...
|
by: iainw |
last post by:
HI All,
1st post here, i wonder if you can help. We are about to upload CMS t
a windows server and keep getting 2 errors below. We need to go LIVE an
it's delaying us.
An error occured when...
|
by: amerar |
last post by:
Hi All,
I posted a question about style sheets, and why certain email clients
were ignoring them. Someone suggested placing them inline. I did
this and get better results, but not what I...
|
by: George W. |
last post by:
Okay, I'm a C#/XML newbie, and I've been wrestling with this for a while
now, checked dotnet sites, articles, MSDN Library, etc. and haven't been
able to determine why this is happening.
I have...
|
by: nfr |
last post by:
I keep getting the following warning in my compile:
Warning: The dependency 'WBWebServices, Version=1.0.1289.13943,
Culture=neutral' in project 'WBWin' cannot be copied to the run directory...
|
by: partybob99 |
last post by:
I am trying to call SP_Password from some vb.net code. This should be
very straight forward but no matter what I do, I keep getting errors.
Here is the code
strConnectString = "Data Source=" +...
|
by: onur karabulut |
last post by:
Hi, I have a simple question for you guys. In my WinForms app, I'm using WndProc to trap certain Windows messages. I sometimes need to block the Main UI-Thread (waiting for a worker thread or a modal...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |