2014-01-16 40 views
-5

如何在C#中拆分字符串。
我写了一个代码,但发现一些像索引和长度的错误必须引用字符串中的位置。参数名:长度
我的代码是如何分割字符串,其中字符串长度> 120在C#

DataTable dt1 = ((DataTable)ViewState["tblsms"]); 



    string smstxt = Convert.ToString(((TextBox)SMSGridView.Rows[e.RowIndex].FindControl("txtsms")).Text.Trim()); 
    int smstext = smstxt.Length; 
    int j = 0; 
    int z= 0; 
    for (int i = 0; i < smstext; i++) 
    { 
     string smstxt1 = string.Empty; 

     if (smstext >= 120) 
     { 
      z = z +120; 
     } 
     else 
     { 
      z = smstext; 
     } 
     smstxt1 = smstxt.Substring(j,z).ToString(); 
     j = j + 120; 


     string title = Convert.ToString(((TextBox)SMSGridView.Rows[e.RowIndex].FindControl("txtTitl")).Text.Trim()); 
     string recptelno = Convert.ToString(((TextBox)SMSGridView.Rows[e.RowIndex].FindControl("txtTelno")).Text.Trim()); 
     string snid = Convert.ToString(((Label)SMSGridView.Rows[e.RowIndex].FindControl("lblsnid")).Text.Trim()); 
     if (snid == "") 
     { 
      snid = "00"; 
     } 
     DataSet ds2 = (DataSet)Session["dsLogIn1"]; 
     string postedbyid = ds2.Tables[0].Rows[0]["usrid"].ToString(); 
     string posttrmid = ds2.Tables[0].Rows[0]["TermID"].ToString(); 
     string postseson = ds2.Tables[0].Rows[0]["SessionID"].ToString(); 


     int ck1 = 0; 

     bool _result1 = Proc1.UpdateRecords("", "SP_ENTRY_SMS", "UPDATESMS", snid, title, smstxt1, recptelno, ck1.ToString(), postedbyid, posttrmid, postseson, "false", "false"); 

     if (!_result1) 
     { 

      this.lblwkMsg.Text = "Message Not Save"; 
      return; 
     } 

     else 
     { 
      this.lblwkMsg.Text = "Message Save Successfully"; 

     } 

     i = i + 120; 

     dt1.Rows[e.RowIndex]["sdescription"] = title; 
     dt1.Rows[e.RowIndex]["smstext"] = smstxt1; 
     dt1.Rows[e.RowIndex]["recptelno"] = recptelno; 
     dt1.Rows[e.RowIndex]["reqflg"] = ck1; 
     //dt1.Rows[e.RowIndex]["postedbyid"] = postedbyid; 
     //dt1.Rows[e.RowIndex]["posttrmid"] = posttrmid; 
     //dt1.Rows[e.RowIndex]["postseson"] = postseson; 
     this.SMSGridView.EditIndex = -1; 
     ViewState["tblsms"] = dt1; 
     this.condition(); 
     this.SMSGridView.DataSource = ViewState["tblsms"]; 
     this.SMSGridView.DataBind(); 

    } 
+3

不要张贴您的完整代码,你得到错误指定的代码仅对该区域 –

回答

0

你可以使用像这样做:

const int len = 120; 
int i = 0; 
var strings = new List<string>(); 

while (str.Length > (i + len)) 
{ 
    strings.Add(str.Substring(i, len); 
    i += len; 
} 

strings.Add(str.Substring(i));