Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
I need single different string value from my resulted string.
My string variable return value like :

string result = 1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1;    //  from here I'll need  2

or  string result = 1,1,1,1,1,1,1,1,4,1,1,1,1,1,1,1;    //  from here I'll need  4

or string result = 1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1;     //  from here I'll need  3


Any suggestion really appreciate !
Posted
Updated 13-Jun-13 21:02pm
v2
Comments
Shine Ashraf 14-Jun-13 4:35am    
Try this,

string result = "1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1";
char[] temp = result.ToCharArray();
string singlText;
foreach (char chr in temp)
{
if (chr == ',')
continue;
int count = result.Count(f => f == chr);
if (count == 1)
{
singlText = chr.ToString();
break;
}
}

You might first split the string using ',' as separator (thus obtaining an array of strings), then you have several options: for instance, you might sort the array and eventually take the last item (or the first one, depending on the actual input values).
 
Share this answer
 
Comments
Mas11 14-Jun-13 3:24am    
Thanks !
CPallini 14-Jun-13 3:29am    
You are welcome.
.split() will work for you..



C#
string result=1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1;  
string strResultValue="";
string[] strArrRestul=result.split(',');

for(int intLoop=0;intLoop<strarrresult.length;intloop++)>
{
    if(strArrResult[intLoop]=="2")
    {
        strResultValue=strArrResult[intLoop];
    }
}



Explanation:
you are just storing your data in string Array with delimiter as comma(,) with the need of split method
then in string array you are searching your desired data..

in split() you can give any delimiter which is in your result variable.
 
Share this answer
 
C#
Hi Its a minor task, but I was don't able to guess its logic. But I have done.


C#
var finalValue = result.Replace(",","").Replace("1","");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900