2009-10-20 6 views
0

Im actuellement avoir des problèmes sur mes codes en C#. Je veux diviser des chaînes sans valeurs fixes. ici 'mon code s'il vous plaît aidez-moi à résoudre ce problème.comment diviser plusieurs chaînes

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

le problème im face est ici quand je rends la page, il affiche une seule valeur de chaîne et ne présente pas la chaîne suivante dans le tableau. Après la division des chaînes avec le "|", vous pouvez commencer par

délimiteur je veux diviser la chaîne avec le nom et l'adresse e-mail avec le délimiteur ":". Comment puis-je faire cela?

Répondre

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

S'il vous plaît vérifier litAuthors.Text = string.Format("{0}<br /><br />", authornames); dans la boucle. u devrait utiliser

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

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

", authornames);

2

Vous pouvez utiliser la méthode String.Join à la place de la boucle foreach:

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

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

EDIT

Je viens de remarquer la deuxième partie de votre question - séparer le nom de l'auteur et adresse e-mail. Vous pouvez revenir à l'aide d'une boucle foreach et faire quelque chose comme ceci:

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

après avoir divisé les cordes avec le « | » délimiteur je veux diviser la chaîne avec le nom et l'adresse e-mail avec le délimiteur ":". comment puis-je faire cela?

Voulez-vous dire cela?

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

Quelque chose comme ceci.

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