0

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?

5 Answers 5

2

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();        
Sign up to request clarification or add additional context in comments.

Comments

1

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

", authornames);

Comments

0
 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);

Comments

0

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]);        
}

Comments

0

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]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.