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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how reorder a string separated by |

Status
Not open for further replies.

yordangs

ISP
Sep 7, 2001
91
MX
Hello to everyone i have the next string
10502=3|10527=2006|10522=1|10504=25000|10505=100|10506=100|10508=25200|10509=200|10514=200|10510=200|10524=200|10525=200|10512=200|10513=200|10535=200|10536=200|10537=200|10516=2000|10517=23200|10518=1000|10519=22200|10520=1000| if you see all the items are separated by | i need to get this tree items |10535=200|10536=200|10537=200 and pass it to the end of the string sometimes the string it will contains this tree items but sometimes can contains two or one item in any case, i have to cut from its original place and put to the end of the string having this result
10502=3|10527=2006|10522=1|10504=25000|10505=100|10506=100|10508=25200|10509=200|10514=200|10510=200|10524=200|10525=200|10512=200|10513=200|10516=2000|10517=23200|10518=1000|10519=22200|10520=1000|10535=200|10536=200|10537=200|

im begining to build a method to do this but im halted i dont know who to do it

here its my code
using System;
namespace Arrays3
{
class Arrays3App
{
public string Reorderchain(string _originalChain)
{
string _newChain = String.Empty;
// Constant for test
_originalChain = "10502=3|10527=2006|10522=1|10504=25000|10505=100|10506=100|10508=25200|10509=200|10514=200|10510=200|10524=200|10525=200|10512=200|10513=200|10535=200|10536=200|10537=200|10516=2000|10517=23200|10518=1000|10519=22200|10520=1000|";
string [] result = _originalChain.Split(new Char[] {'|'});
for (int i = 0; i < result.Length; i++)
{
result;
}
// here i need to return the new chain
return _newChain;
}
}
}

any help will be apreciated a lot thanks anyway i hope anyone can help me :D


 
well i solve the problem this way

string test = "10502=3|10527=2006|10522=1|10504=3000|10508=3000|10509=100|10514=100|10510=100|10524=100|10525=100|10512=100|10513=100|10535=100|10516=1000|10537=100|10517=2000|10520=2000";
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int f=0;
int g=0;
int h=0;
int i=0;
int x=0;
int y=0;
int z=0;
int sumatoria=0;
string Corte1=String.Empty;
string Corte2=String.Empty;
string Corte22=String.Empty;
string Corte3=String.Empty;
string Corte33=String.Empty;
string Corte4=String.Empty;
string Corte44=String.Empty;
string Corte5=String.Empty;
string _cadenaNueva=String.Empty;
string _pipeFinal=String.Empty;
string _pipeIntermedio = String.Empty;
int ultimopipe=0;
int midecadenaoriginal=0;
int ultimoPipeCadenaNueva;
int mideCadenaNueva=0;
try
{
a = test.IndexOf("10535");
b = test.IndexOf("10536");
c = test.IndexOf("10537");
if(a<=0){d=0;}
else{d = test.IndexOf("|",a);}
if(b<=0){e=0;}
else{e = test.IndexOf("|",b);}
if(c<=0){f=0;}
else{f = test.IndexOf("|",c);}
// Length para cortes
g = (d - a);
h = (e - b);
i = (f - c);
_pipeFinal="|";
// Validamos desde donde vamos a empezar el corte
if (a<=0)
{
if(b<=0)
{
if(c<=0)
{
Corte1=test;
}
else{
Corte1 = test.Substring(0,c);
}
}
else{
Corte1 = test.Substring(0,b);
}
} // if a
else {
Corte1 = test.Substring(0,a);
}
// Verificamos si traemos cada uno de los conceptos
if(g>=5){Corte2 = test.Substring(a-1,g+1); // ## Si traemos concepto 10535
x=Corte2.Length;
Corte2 = test.Substring(a,g+1);
}
if(h>=5){Corte3 = test.Substring(b,h+1); // ## Si traemos concepto 10536
y=Corte3.Length;
}
if(i>=5){Corte4 = test.Substring(c,i+1); // ## Si traemos concepto 10537
z=Corte4.Length;
}
if(g<5)
{
if(h<5)
{
if(i<5)
{
Corte5 =String.Empty;
_pipeFinal=String.Empty;
}
else
{
sumatoria=(x+y+z);
if(sumatoria >0)
{
Corte5 = test.Substring(c+x+y+z);
}
}
}
else
{
sumatoria=(x+y+z);
if(sumatoria >0)
{
Corte5 = test.Substring(b+x+y+z);
}
}
}
else
{
sumatoria=(x+y+z);
if(sumatoria >0)
{
Corte5 = test.Substring(a+x+y+z);
}
}
// Verificamos que el ultimo caracter sea | sino lo agregamos para que haga el corte bien
ultimopipe = test.LastIndexOf("|");
midecadenaoriginal = (test.Length - 1);
if(ultimopipe != midecadenaoriginal ){_pipeIntermedio ="|";}
// Si el ultimo Caracter es | lo quitamos de la cadena
_cadenaNueva = Corte1 + Corte5 + _pipeIntermedio + Corte2 + Corte3 + Corte4 ;
ultimoPipeCadenaNueva =_cadenaNueva.LastIndexOf("|");
mideCadenaNueva = (_cadenaNueva.Length - 1);
if(ultimoPipeCadenaNueva == mideCadenaNueva)
{
_cadenaNueva =_cadenaNueva.Substring(0,ultimoPipeCadenaNueva);
}

}
catch (SystemException ex)
{
Console.WriteLine(ex);
}

if anyone have a better idea i will apreciated grettings
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top