在 Console.WriteLine 方法中格式化字符串

2

本文介绍了在 Console.WriteLine 方法中格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 C# 编程新手.谁能解释一下下面的代码:

Im new to C# programming. Can someone please explain the following code:

Console.WriteLine( "{0}{1,10}", "Face", "Frequency" ); //Headings
Console.WriteLine( "{0,4}{1,10}",someval,anotherval);

我知道这会打印带有给定标题的两列值,并且 {0} 指的是给定的第一个参数.但是 {x,y} 形式的格式字符串是什么意思呢?

I understand that this prints two columns of values with the headings given, and {0} refers to the first argument given. But what is the meaning of the format strings of the form {x,y} ?

推荐答案

向左添加填充.以下备忘单对于记住各种字符串格式模式非常有用:

It adds padding to the left. Very useful for remembering the various string formatting patterns is the following cheat sheet:

.NETString.Format 备忘单

正值向左添加内边距,负数向右侧添加内边距

Positive values add padding to the left, negative add padding to the right

Sample                                 Generates
String.Format("[{0, 10}]", "Foo");     [Foo]
String.Format("[{0, 5}]", "Foo");      [Foo]
String.Format("[{0, -5}]", "Foo");     [Foo]
String.Format("[{0, -10}]", "Foo");    [Foo]

这篇关于在 Console.WriteLine 方法中格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14