最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

字符串插值 C#:冒号和分号功能的文档

网站源码admin93浏览0评论
本文介绍了字符串插值 C#:冒号和分号功能的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在 FizzBu​​zz 测试中找到了这个 codegolf 答案,稍微检查了一下之后我意识到我不知道它实际上是如何工作的,所以我开始调查:

I found this codegolf answer for the FizzBuzz test, and after examining it a bit I realized I had no idea how it actually worked, so I started investigating:

for(int i=1; i<101;i++)
    System.Console.Write($"{(i%3*i%5<1?0:i):#}{i%3:;;Fizz}{i%5:;;Buzz}\n");

我把它放入 dotnetfiddle 并建立第一部分的工作如下:

I put it into dotnetfiddle and established the 1st part works as follows:

{(BOOL?0:i):#}

当 BOOL 为真时,条件表达式返回 0,否则返回数字.

When BOOL is true, then the conditional expression returns 0 otherwise the number.

然而,除非是 <>,否则不会返回数字.0.我猜这是 :# 字符的工作.我找不到关于 :# 字符工作的任何文档.任何人都可以解释冒号/哈希或指出正确的方向吗?

However the number isn't returned unless it's <> 0. I'm guessing this is the job the of :# characters. I can't find any documentation on the :# characters workings. Can anyone explain the colon/hash or point me in the right direction?

第二部分:

{VALUE:;;Fizz}

VALUE = 0 时,则不打印任何内容.我假设这是由第一个 ; 字符 [end statement] 决定的.第二个 ; 字符确定 'if VALUE <>0 然后打印我之后的内容.'

When VALUE = 0 then nothing is printed. I assume this is determined by the first ; character [end statement]. The second ; character determines 'if VALUE <> 0 then print what's after me.'

再说一次,有没有人有关于在字符串插值中使用 分号 的文档,因为我找不到任何有用的东西.

Again, does anyone have documentation on the use of a semicolon in string interpolation, as I can't find anything useful.

推荐答案

感谢所有评论者!快速响应.

Thanks to all the commenters! Fast response.

# 在这里定义(自定义说明符)

https://docs.microsoft/en-us/dotnet/standard/base-types/custom-numeric-format-strings#the--custom-specifier

#"自定义格式说明符用作数字占位符.如果正在格式化的值在其中的位置有一个数字#"符号出现在格式字符串中,该数字被复制到结果字符串.否则,在该位置不会存储任何内容结果字符串.请注意,此说明符从不显示零不是有效数字,即使零是唯一的数字细绳.仅当它是有效数字时才会显示零正在显示的号码.

The "#" custom format specifier serves as a digit-placeholder symbol. If the value that is being formatted has a digit in the position where the "#" symbol appears in the format string, that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string. Note that this specifier never displays a zero that is not a significant digit, even if zero is the only digit in the string. It will display zero only if it is a significant digit in the number that is being displayed.

;在此处定义(节分隔符):

https://docs.microsoft/en-us/dotnet/standard/base-types/custom-numeric-format-strings#the--section-separator

分号 (;) 是一个条件格式说明符,适用于不同的数字格式取决于其值是否为正、负或零.为了产生这种行为,自定义格式字符串最多可以包含三个以分号分隔的部分...

The semicolon (;) is a conditional format specifier that applies different formatting to a number depending on whether its value is positive, negative, or zero. To produce this behavior, a custom format string can contain up to three sections separated by semicolons...

这篇关于字符串插值 C#:冒号和分号功能的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

发布评论

评论列表(0)

  1. 暂无评论