gpt4 book ai didi

string - 为什么 "Here Strings"被称为 "Here Strings"?

转载 作者:行者123 更新时间:2023-12-01 07:24:31 26 4
gpt4 key购买 nike

这个名字背后的原因是什么?
SS64在 PowerShell 中解释此处的字符串如下:

A here string is a single-quoted or double-quoted string which can span multiple lines.Expressions in single-quoted strings are not evaluated.

All the lines in a here-string are interpreted as strings, even though they are not enclosed in quotation marks.

$myHereString = @'
some text with "quotes" and variable names $printthis
some more text
'@

最佳答案

它们在 PowerShell 中有这个名称,因为它是从 Unix 风格的 shell 中借来的,就像 PowerShell 中的许多其他元素和概念一样:
From Wikipedia :

Here documents originate in the Unix shell, and are found in sh, csh, ksh, bash and zsh, among others.


至于为什么最初使用这个名字,文章并没有严格涵盖词源,而是基于这样的描述:

In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text.


名称的“here”部分指的是包含“here”的文件(如此时),这似乎合乎逻辑。

为了完整起见,值得注意的是,PowerShell 还直接支持单引号和双引号中的换行符 [string] s,像这样:
$myString = 'some text with "quotes" and variable names $printthis 
some more text'
这里字符串有用的一个更好的例子是当你需要两种类型的引号时:
$myHereString = @'
some text with "quotes" and variable names $printthis
some more text
and my grandma's soup
'@
如果这只是一个单引号字符串,则 'grandma's将需要逃脱。
如果是双引号字符串,则需要转义 " 的实例。你需要逃离 $如果你不想变量扩展。

有趣的是:此处字符串的解释方式之间的差异构成了 this demonstration of writing a bash/powershell/win batch polyglot 的基础(其内容可以在任何这些环境中执行的脚本)。

关于string - 为什么 "Here Strings"被称为 "Here Strings"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41454502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com