2013-11-26 16 views
1

我目前正在嘗試修改SSIS項目的腳本,以從動態.txt文件(有時添加colums)導入數據。c#中的string.format語法和它做了什麼?

我新的C#和腳本唯一的問題是,它包含一個字符串形式表達,我真的不能graps所以我的問題是:

這是什麼呢?

string _Statement = String.Format ("{0}(?=(?:[^{1}]*{1}[^{1}]*{1})*(?![^{1}]*{1}))" 

的參數是

{0} = First line in the file containing all the column headers 
    {1} = Semicolon delimiter 

在任何幫助,這將非常感激。

回答

0

String.Format方法這裏取代的{0} First line in the file containing all the column headers所有出現和{1所有出現}與Semicolon delimiter。在這種情況下,結果字符串將是一個正則表達式(正則表達式)。

+0

謝謝你解釋它!我想這使問題錯誤,因爲它是正則表達式,我不明白,而不是string.format。現在我有一些工作與alteast! – user3036771

3

由於重複使用相同的索引,因此會插入相同的值。

所以{0}

First line in the file containing all the column headers 

更換和{1}所有出現將

Semicolon delimiter 

但是被替換,上面的代碼似乎是不完整的,因爲我缺少的參數列表在string.Format的末尾。類似:

string _Statement = String.Format("{0}(?=(?:[^{1}]*{1}[^{1}]*{1})*(?![^{1}]*{1}))" 
            , firstLine , ";"); 

。假定該線是:TEST-LINE這是結果:

"TEST-LINE(?=(?:[^;]*;[^;]*;)*(?![^;]*;))" 
+0

似乎由於缺乏聲譽我不能贊成,但這個答案解釋了這個問題。謝謝! – user3036771

+0

@ user3036771:你爲什麼不接受它,有什麼不對嗎? –

0

String.Format格式化您的字符串,因此{}中的每個參數都將替換爲您也必須指定的值。在你的榜樣,用{0}將

First line in the file containing all the column headers 

被取代,所有的值與{1}將

Semicolon delimiter 

更換所有的值你必須作爲參數傳遞給String.Format指定此兩個值,所以你的代碼應該是這樣的:

string first = "col1,col2,col3"; 
string second = ";"; 
string _Statement = String.Format("{0}(?=(?:[^{1}]*{1}[^{1}]*{1})*(?![^{1}]*{1}))", first, second); 

而其結果將是

_Statement = "col1,col2,col3(?=(?:[^;]*;[^;]*;)*(?![^;]*;))";