Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public string display()
    {
        string st="";
        DataTable dt= (DataTable)ViewState["CurrentData"];
        for (int i = 0; i < dt.Rows.Count ; i++)
        {
            st += dt.Rows[i]["item"].ToString() + ",";
        }

        return st;
    }

This is my function who stores values in string format
for eg:- abc@gmail.com,xya@yahoo.com,dfd@gmail.com,

How can i remove the last comma from the string
for eg:- abc@gmail.com,xya@yahoo.com,dfd@gmail.com

Pls help me...
Posted

C#
string finalvalue = string.Empty;
finalvalue = lblSelected.Text.Substring(0, lblSelected.Text.Length - 1);


Thanks
Ashish
 
Share this answer
 
st = st.Remove(st.Length - 1, 1);
 
Share this answer
 
st=st.Substring(0,st.Length-1);
 
Share this answer
 
string[] strArr = null;
st = "CSharp,split,test";
char[] splitchar = {','};
strArr = st.Split(splitchar);

use this to remove comma......
 
Share this answer
 
v2
Comments
pradiprenushe 14-Aug-12 8:26am    
Correct.
C#
public string display()
    {
        string st="";
        DataTable dt= (DataTable)ViewState["CurrentData"];
        for (int i = 0; i < dt.Rows.Count ; i++)
        {
            st += dt.Rows[i]["item"].ToString() + ",";
        }
        st = st.LastIndexOf(",") == st.Length - 1 ? st.Substring(0, st.Length - 1) : st;
        return st;
    }



Thanks,

Kuthuparakkal
 
Share this answer
 
v3
C#
public string display()
    {
        string st="";
        DataTable dt= (DataTable)ViewState["CurrentData"];
        for (int i = 0; i < dt.Rows.Count ; i++)
        {
          if(i==dt.Rows.Count-1)
          {
             st += dt.Rows[i]["item"].ToString() ;
          }
          else
          {
             st += dt.Rows[i]["item"].ToString() + ",";
          }
       }

        return st;
    }
 
Share this answer
 
just do substring while returning.

C#
return st.Substring(0,st.Length-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