2016-02-26 38 views
-2

我正在轉換VB應用程序。我正在嘗試使用StringBuilder創建一個字符串,因爲我無法使用Mid函數更改字符串。我有一個循環,雖然它只是將第一個字符添加到字符串,其餘的只是空白。我如何將所有數據添加到字符串?在C#中使用StringBuilder創建字符串

下面的代碼片段;感謝您的幫助

 int table = 0; 
     string tableData = Strings.StrDup(253, " "); 
     int i = 0; 
     string listData = null; 
     int pointer =0; 
     StringBuilder sb = new StringBuilder(tableData); 
     sb.Insert(0, Strings.StrDup(253, Strings.Chr(32))); 
     sb.Insert(0, typeOfTable + Strings.Chr(0)); 


     pointer = 2; 
     for (i = 0; i <= lstTABLE.Items.Count -1; i++) 
     { 
      listData = lstTABLE.Items[i].ToString(); 
      table = Convert.ToInt32(-(Conversion.Val(listData) * 10)); 
      sb.Insert(pointer, Functions.FNCodeTwoChar(table)); 
      pointer +=2; 
     } 

     sb.Insert (202,Functions.EncodeKP(Convert.ToSingle(Conversion.Val(lblStartTable.Text)))); 
     sb.Insert(205,Functions.EncodeKP(Convert.ToSingle(Conversion.Val(lblEndTable.Text)))); 
     sb.Insert(208, Strings.Space(36)); 
     sb.Insert(244, " 0"); 
     sb.Insert(248, " 0"); 
+3

問題是? –

+0

爲什麼你使用'StringBuilder.Insert',要求你管理愚蠢的字符指針?使用['StringBuilder.Append'](https://msdn.microsoft.com/en-us/library/system.text.stringbuilder.append.aspx)! – poke

+0

謝謝,但沒有必要投票 – Russ

回答

2

的回答了這個問題:「我有一個循環,雖然它只是增加了第一個字符的字符串,剩下的只是空白。我如何都可以將數據添加到字符串? 「是:

您添加到字符串的第二個字符是字符串結束符字符串Strings.Chr(0)。當C#(或VB.Net)遇到這個字符時,它會停止讀取字符串。

相關問題