我試圖使用動態ammount在單詞之間製作一個簡單的選項卡空間。我將如何做到這一點?多次將相同的值添加到字符串中
只要是這樣的:
string p1 = keysC[pos]+"="+valsC[pos];
int tabs = (60 - p1.Length)/4;
wr.WriteLine(p1 + ("\t" * tabs) +"//"+comsC[pos]);
我試圖使用動態ammount在單詞之間製作一個簡單的選項卡空間。我將如何做到這一點?多次將相同的值添加到字符串中
只要是這樣的:
string p1 = keysC[pos]+"="+valsC[pos];
int tabs = (60 - p1.Length)/4;
wr.WriteLine(p1 + ("\t" * tabs) +"//"+comsC[pos]);
的String
contructor有出過載。
string p1 = keysC[pos]+"="+valsC[pos];
int tabs = (60 - p1.Length)/4;
wr.WriteLine(p1 + new string('\t', tabs) +"//"+comsC[pos]);
注意構造函數的第一個參數是char
而不是string
。
聽起來像是隻需要編寫一個重複的次數相同的字符串N多
static string RepeatString(string source, int times) {
var builder = new StringBuilder(source.Length * times);
for (int i = 0; i < times; i++) {
builder.Append(source);
}
return builder.ToString();
}
請注意,如果你只關心重複char
像\t
然後使用@的new string(theChar, theCount)
+1。甚至'的string.join(NULL,Enumerable.Repeat(源,倍));' –
@AlexeiLevenkov:這將是比構造效率較低。 –
@TimSchmelter - 實際上對於單個字符,因爲在實際OP的情況下,應該使用構造函數。 –
您可以使用string
constructor:
string allTabs = new string('\t', tabs);
試試這個:
string spacing = new String('\t', tabs);
你在問什麼?什麼不行?預期的結果是什麼?這是一個問題還是答案? – Cosmin
附註:不是每個人都有標籤設置爲4(我相信在大多數情況下,默認實際上是8)。考慮使用空格填充(幾乎相同的代碼,但在更多情況下可以正確顯示)。 –