簡體   English   中英

c#中的string.format語法及其作用?

[英]string.format syntax in c# and what it does?

我目前正在嘗試修改SSIS項目的腳本以從動態.txt文件導入數據(有時會添加colums)。

我是c#的新手,腳本中唯一的問題是它包含一個我真的無法抓取的字符串格式表達式,所以我的問題是:

這是做什么的?

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}將替換為

 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(?=(?:[^;]*;[^;]*;)*(?![^;]*;))"

此處的String.Format方法將所有出現的{0}替換為First line in the file containing all the column headers以及所有出現的帶有Semicolon delimiter的{1}。 在這種情況下,結果字符串將是正則表達式(正則表達式)。

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(?=(?:[^;]*;[^;]*;)*(?![^;]*;))";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM