tags:

views:

158

answers:

6

Hi guys, im currently having troubles on my codes in C#. I want to split strings with no fix values. here' my code please help me fix this.

protected void GridViewArchives_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView drView = (DataRowView)e.Row.DataItem;
        Literal litAuthors = (Literal)e.Row.FindControl("ltAuthors");

        string authors = drView["Author(s)"].ToString();
        //authors = Trent Riggs:[email protected]|Joel Lemke:[email protected]
        string[] splitauthors = authors.ToString().Split("|".ToCharArray());


        foreach (string authornames in splitauthors)
        {
            litAuthors.Text = string.Format("{0}<br /><br />", authornames);
        }
    }
}

the problem im facing here is when i render the page it only displays one string value and does not display the succeeding string in the array.

after splitting the strings with the "|" delimeter i want to split the string with name and email address with the delimeter ":". how do i do this?

+1  A: 

litAuthors.Text += string.Format("{0}

", authornames);

Alex Reitbort
A: 
 foreach (string authornames in splitauthors)
        {
            litAuthors.Text = string.Format("{0}<br /><br />", authornames);
        }

Please check litAuthors.Text = string.Format("{0}<br /><br />", authornames); inside the for loop. u should use

 litAuthors.Text +=  string.Format("{0}<br /><br />", authornames);
Himadri
+2  A: 

You could use the String.Join method instead of your foreach loop:

string authors = drView["Author(s)"].ToString();
string[] splitAuthors = authors.Split('|');

litAuthors.Text = string.Join("<br /><br />", splitAuthors) + "<br /><br />";

EDIT

I just noticed the second part of your question - separating out the author's name and email address. You could go back to using a foreach loop and do something like this:

string authors = drView["Author(s)"].ToString();
string[] splitAuthors = authors.Split('|');

StringBuilder sb = new StringBuilder();
foreach (string author in splitAuthors)
{
    string[] authorParts = author.Split(':');

    sb.Append("Name=").Append(authorParts[0]);
    sb.Append(", ");
    sb.Append("Email=").Append(authorParts[1]);
    sb.Append("<br /><br />");
}
litAuthors.Text = sb.ToString();
LukeH
A: 

after splitting the strings with the "|" delimeter i want to split the string with name and email address with the delimeter ":". how do i do this?

Do you mean this?

foreach (string authornames in splitauthors)        
{            
string[] authorDetails = authornames.Split(':');
litAuthors.Text += string.Format("{0}<br /><br />", authorDetails[0]);        
}
Town
A: 

Something like this.

string authors = drView["Author(s)"].ToString();
//authors = Trent Riggs:[email protected]|Joel Lemke:[email protected]
string[] splitauthors = authors.ToString().Split("|".ToCharArray());


foreach (string author in splitauthors)
{
    string[] emailNameSplit = author.Split(":".ToCharArray());
    litAuthors.Text += string.Format("Name: {0} Email: {1}<br /><br />", emailNameSplit[0], emailNameSplit[1]);
}
Oscar Kilhed
A: 

tnx for all posts! it's a very big help! xD

nhoyti