Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reverse order of a parsed string

Status
Not open for further replies.

onressy

Programmer
Mar 7, 2006
421
CA
Hi, I was wondering if someone could give me a hand, I'm not a C# programmer, but an ASP/VB programmer. I need to do one thing in C#, I have a string like so:

Token A#$%@&Token B#$%@&Token C#$%@&Token D#$%@&Token E

I need to to split the string at #$%@ and then reassemble the strings and delimiters into a new string where each of the substrings are in the revers order from the original string, and ofcourse return the final string.

Sorry if i'm asking a lot, if this was VB, i would use split function, not sure how to do it in C#, Thanks again!
 
i would use split function

> Then use it:

string String = "Token A#$%@&Token B#$%@&Token C#$%@&Token D#$%@&Token E";
string[] result = String.Split(...);


Regards
PS: I'm a VB programmer too.
 
Thanks, ok i getting the hang of this,

if i have to put the substrings in reverse order would i do something like:

string[] ar = Regex.Split(text, "#$%@");

for (int a=0; a<ar.Length; a--)
{
System.Console.WriteLine(ar);
}

 
OK am i getting closer to reversing the substrings of the array:
Token A#$%@&Token B#$%@&Token C#$%@&Token D#$%@&Token E

so the result is:
Token E#$%@&Token D#$%@&Token C#$%@&Token B#$%@&Token A
-----

string[] ar = Regex.Split(text, "#$%@");

Array.Reverse(ar);
foreach (string strAr in ar)
{
fullString = fullString + ar + "#$%@");
}


// then remove the last #$%@

fullString = Left(fullString,Len(fullString)-4)

System.Console.WriteLine(fullString);

 
sorry, this is what I ment, Is it correct?


string[] ar = Regex.Split(text, "#$%@&");

Array.Reverse(ar);
foreach (string strAr in ar)
{
fullString = fullString + strAr + "#$%@&");
}


// then remove the last #$%@&

fullString = Left(fullString,Len(fullString)-5)

System.Console.WriteLine(fullString);
 
does it work?

Age is a consequence of experience
 
Onressy hi.

The way i suggested with the split is much much complicated, because it splits using a char and not a string. Use what cappmgr said earlier.
To help you more, although i dont use C#, Try the next:

Code:
   [b]using System.Text.RegularExpressions;[/b]

   string source = "Token A#$%@&Token B#$%@&Token C#$%@&Token D#$%@&Token E";
   string del = "#$%@&";

   Regex r = new Regex(del);
   string[] stage1 = r.Split(source);
   System.Array.Reverse(stage1);

   string newSource = string.Join(del, stage1);
   MessageBox.Show(newSource);



Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top