2017-02-04 94 views
1

我想問一下Console.WriteLine()方法中的負向對齊格式。據我所知,當你寫一個正值,例如Console.WriteLine("{0,5}",123),如果參數「spacing」小於你輸入的對齊部分,它會用空格填充它的左邊。c#負向對齊格式化

但我不明白消極的調整。例如Console.WriteLine("{0,-5}",123)會做什麼?我沒有看到Console.WriteLine("{0,-5}",123)Console.WriteLine("{0}",123)之間的任何區別。請一些解釋。 PS:一個例子會使它更容易。

回答

2

使用您提供的示例很難理解,因爲負對齊組件會在該單詞之後添加空格(如果對齊的絕對值大於該單詞的長度),因此您可能看不到這些空格除非你有以下的東西。

實施例:

考慮字(堆棧 => 5個字符和溢出 => 8個字符)

Console.WriteLine("{0,-5}{1,8}\n", "Stack", "Overflow"); // StackOverflow 
Console.WriteLine("{0,-6}{1,8}\n", "Stack", "Overflow"); // Stack Overflow 
Console.WriteLine("{0,-6}{1,9}\n", "Stack", "Overflow"); // Stack Overflow 
+0

所以基本上消極對齊增加了單詞右側的空白區域,如果它是積極的,它會添加到左側? – john

+0

.................................. – john

+0

@john是的,正好(增加了單詞的長度條件做出完整的描述)。 – user3185569

1

可以findout更多有關的String.Format像

string.Format("LEFT: string: ->{0,-5}<- int: ->{1,-5}<-", "abc", 123); 
string.Format("RIGHT: string: ->{0,5}<- int: ->{1,5}<-", "abc", 123); 

輸出:

LEFT: string: ->abc <- int: ->123 <- 
RIGHT: string: -> abc<- int: -> 123<-