标签: string-interpolation

如何在Perl字符串中手动插入字符串转义?

在perl中假设我有一个字符串'hello\tworld\n',我想要的是:

'hello  world
'
Run Code Online (Sandbox Code Playgroud)

也就是说,"你好",然后是文字制表符,然后是"世界",然后是文字换行符.或者等效地"hello\tworld\n"(注意双引号).

换句话说,是否有一个函数用于获取带有转义序列的字符串并返回一个等效的字符串,其中插入了所有转义序列?我不想插入变量或其他任何东西,只是转义序列,比如\x,哪里x是一个字母.

perl escaping string-interpolation

5
推荐指数
1
解决办法
1677
查看次数

用最近的gsubfn打破字符串插值?

最后一个例子用来工作,现在却没有.函数内部没有字符串插值?也许它与变量范围有关?有什么建议?

library(gsubfn)
#R.Version() # I'm using 2.15.0, just upgraded from 2.13.something.

### dumb example of a function
g <- function() {for (a in 1:2) { print(paste('is a', a)) }}
g()

### same thing, outside a function, using string interpolation
for (a in 1:2) { fn$print('is a $a') }
rm(a)

### and now string interpolation inside a function
h <- function() {for (a in 1:2) { fn$print('is a $a') }}
h()
Run Code Online (Sandbox Code Playgroud)

最后一个例子告诉我

Error in eval(expr, envir, enclos) : object 'a' not …
Run Code Online (Sandbox Code Playgroud)

debugging r string-interpolation gsub

5
推荐指数
0
解决办法
449
查看次数

在命令中使用变量(PowerShell)

$computer = gc env:computername

# Argument /RU '$computer'\admin isn't working.
SchTasks /create /SC Daily /tn "Image Verification" /ST 18:00:00 /TR C:\bdr\ImageVerification\ImageVerification.exe /RU '$computer'\admin /RP password
Run Code Online (Sandbox Code Playgroud)

基本上我需要在计划任务中提供计算机名称...

先感谢您!

variables powershell quoting string-interpolation

5
推荐指数
1
解决办法
1万
查看次数

在字符串中访问对象的方法

最近我正在阅读php文档,并在字符串部分找到了有趣的注释:

自PHP 5起,{$}内的函数,方法调用,静态类变量和类常量工作.但是,访问的值将被解释为定义字符串的作用域中变量的名称.使用单个花括号({})将无法访问函数或方法的返回值或类常量或静态类变量的值.

www.php.net/manual/en/language.types.string.php

它说,我不能使用卷曲语法来获取object的方法调用返回的值.这是手动错误还是我误解了它,因为我尝试了下面的代码,它运行得很好:

<?php
class HelloWorld
{
    public static function hello() 
    {
        echo 'hello';
    }
}
$a = new HelloWorld();

echo "{$a->hello()} world";
Run Code Online (Sandbox Code Playgroud)

php string string-interpolation

5
推荐指数
1
解决办法
200
查看次数

R String解释:为什么"\ 040"被解释为""以及我在字符串解释中可能遇到的其他潜在陷阱?

我今天正在帮助某人正式使用我们作为txt文件读取的pdf文件中的一些信息.不幸的是,当时tm包readPDF函数无法正常工作,但通过一些修改我们能够让它工作得很好.当我们从.txt文件中重新出现一些漏洞时,我们发现了一些令我们大多数人感到惊讶的东西,即字符串"\ 040"被解释为空格"".

> x <- "\040"    
> x    
> [1] " "
Run Code Online (Sandbox Code Playgroud)

对于您可能期望发生这种情况的其他相似字符串(即"\n"或"\ t"),不会发生这种情况.

> y <- "\n"   
> y    
> [1] "\n"    
> z <- "\t"    
> z    
>[1] "\t"
Run Code Online (Sandbox Code Playgroud)

为什么是这样?在R中,哪些其他字符串的解释方式不同?

编辑:

在简单的实验之后,任何"\ xxx"(其中x是数字)都会产生不同的结果.这有什么价值?

string r string-interpolation

5
推荐指数
1
解决办法
3652
查看次数

新的C#6.0字符串插值语句不编译

我正在尝试实现此视频中的一些功能,但我对新的字符串插值语法没有太多运气(我还有其他所有工作,至少在这段代码中是这样).

我正在使用Visual Studio 2015 CTP6,我已将其配置为使用.NET 4.6并已进入Build选项以确保我指定C#6.0.我也按照这里的说明进行操作.

这是我的代码:

using System;
using static System.Math;

namespace NewCsharp6Features
{
    public class C6Point
    {
        public int X { get; }
        public int Y { get; }
        public double Distance => Sqrt(X * X + Y * Y);

        public C6Point(int x, int y) { X = x; Y = y; }

        public override string ToString()
        {
            return "(\{X}, \{Y})";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到了两个这样的编译错误:

CS1009 | 无法识别的转义序列

知道我做错了什么,在这里?

c# string-interpolation c#-6.0 visual-studio-2015

5
推荐指数
1
解决办法
726
查看次数

字符串插值的符号名称

令我惊讶的是,这不起作用 -

implicit class LambdaContext(val sc: StringContext) extends AnyVal {
  def ?(args: Any*) = args
}

scala> ?"?x.x"
<console>:1: error: ';' expected but string literal found.
?"?x.x"
Run Code Online (Sandbox Code Playgroud)

Though it's a valid method name:

scala> new LambdaContext(new StringContext("?x.x")).?()
res1: Seq[Any] = List()
Run Code Online (Sandbox Code Playgroud)

Not sure if it's a bug or simple restriction.

scala string-interpolation

5
推荐指数
1
解决办法
63
查看次数

字符串插值中可避免的装箱

使用字符串插值使我的字符串格式看起来更加清晰,但是.ToString()如果我的数据是值类型,我必须添加调用。

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var person = new Person { Name = "Tom", Age = 10 };
var displayText = $"Name: {person.Name}, Age: {person.Age.ToString()}";
Run Code Online (Sandbox Code Playgroud)

.ToString()使得格式更长更难看。我试图摆脱它,但它string.Format是一个内置的静态方法,我无法注入它。你对此有什么想法吗?而且既然字符串插值是 的语法糖string.Format,为什么.ToString()在生成语法糖背后的代码时不添加调用呢?我认为这是可行的。

.net c# string string-interpolation

5
推荐指数
1
解决办法
581
查看次数

Angular 2 模型函数插值 - self.context.$implicit.X 不是函数

全部,

尝试绑定到模型上的函数时出现错误

我有以下组件、模型和 Html(对于这个例子都是假的)

export class TestModel {    
  public getSomeValue(): string {
     return "Hello World";
  }
}
Run Code Online (Sandbox Code Playgroud)

让我们假设 testModels 属性以某种方式获取它的数据。

@Component(...)
public class MyComponent {    
  public testModels: TestModel[];

  ngOnInit() {
    this.loadData();
  }

  public loadData(): void
  {
     this.http.get("...")
     .map(r=>r.json as TestModel[]) // <--- I think now you've asked the question, I see the problem on this line
     .subscribe(d=>this.testModels = d);

  }
}
Run Code Online (Sandbox Code Playgroud)

现在在我的 html

<div *ngFor="let testModel of testModels">
  <span>{{testModel.getSomeValue()}}</span> <!-- This fails -->
</div>
Run Code Online (Sandbox Code Playgroud)

这是完整的错误:

error_handler.js:48 EXCEPTION: Error in ./MyComponent …

string-interpolation typescript angular2-template angular

5
推荐指数
1
解决办法
3200
查看次数

Perl6中的字符串插值

我很难搞清楚为什么声明

say "\c500";
Run Code Online (Sandbox Code Playgroud)

按预期在屏幕上生成字符'Ǵ',而以下语句在编译时给出错误消息("无法识别的\ c字符"):

my $i = 500;
say "\c$i";
Run Code Online (Sandbox Code Playgroud)

即使

say "$i"; # or 'say $i.Str;' for that matter
Run Code Online (Sandbox Code Playgroud)

产生"500"(带"$ i".WHAT表示类型为Str).

string-interpolation perl6

5
推荐指数
2
解决办法
192
查看次数