2009-10-20 3 views
0

메신저에서 현재 내 C#에 문제가 발생했습니다. 고정 값없이 문자열을 분할하고 싶습니다. 여기 '내 코드가이 문제를 해결하도록 도와주세요. 내가 페이지가 하나 개의 문자열 값을 표시하고 배열의 성공 문자열을 표시하지 않습니다 렌더링 할 때여러 문자열을 분할하는 방법

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

여기에 직면하고있는 문제의 메신저입니다.

문자열을 "|" delimeter delimeter ":"와 함께 이름과 이메일 주소로 문자열을 분할하고 싶습니다. 어떻게해야합니까?

답변

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

루프 내부 litAuthors.Text = string.Format("{0}<br /><br />", authornames); 을 확인하시기 바랍니다. 유

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

litAuthors.Text + = 및 String.format ("{0}

"authornames)를;

2

당신은 대신 foreach 루프의 String.Join 방법을 사용할 수 사용해야합니다 : 난 그냥 질문의 두 번째 부분을 발견

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

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

편집 - 아웃 분리 저자 이름과 이메일 주소. 당신은 foreach 루프를 사용하여 다시 가서 같은 것을 할 수있는 다음 로 문자열을 분할 한 후

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

를 "|" delimeter 나는 delimeter ":"로 문자열을 이름과 이메일 주소 으로 나누고 싶다. 어떻게해야합니까 ?

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

이와 비슷한 것입니다.

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]); 
} 
관련 문제