2016-09-21 61 views
-1

在這裏我想根據分隔符的號碼來建立一個字符串。 我的方案, ListOfWords是ABC | EFG然後建設者爲1/2,但在myone它只顯示1根據分隔符的號碼建立一個字符串

我的C#代碼是

var ListOfWords = "ABC|EFG"; 
var noOfOfSeperators = ListOfWords.Count(f => f == '|'); // in here it shows me 1 
StringBuilder builder = new StringBuilder(); 
for (int i = 1; i <= noOfOfSeperators; i++) { 
    builder.Append(i).Append("/"); // i want to show the final result if this is 1/2 
} 
+0

你面臨什麼問題? –

+2

noOfSeperators + 1? –

+0

@DarrenYoung,更確切地說:'for(int i = 1; i <= noOfOfSeperators + 1; i ++)' –

回答

1
var ListOfWords = "ABC|EFG|HIJ"; 

var noOfOfSeperators = ListOfWords.Count(f => f == '|'); 

StringBuilder builder = new StringBuilder(); 
for (int i = 0; i <= noOfOfSeperators; i++) 
{ 
    if(builder.Length>0) 
    { 
     builder.Append("/"); 
    } 
    builder.Append(i+1); 
} 

var newString = builder.ToString(); 

OUTPUT:

1/2/3

相關問題