标签: string-interpolation

在Clojure中使用String Interpolation宏(<<)

刚刚开始使用clojure.我正在使用leiningen并且无法弄清楚为什么导入<<宏似乎不起作用

project.clj

(defproject myapp "0.1"
  :description "Clojure learning sandbox"
  :main myapp.core
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [org.clojure/core.incubator "0.1.2" ]])
Run Code Online (Sandbox Code Playgroud)

core.clj

(ns clojure-shuffle
  (:require [clojure.core.incubator :refer [<<]]))

(defn -main [& args]
  (println (<< "The sum is: ~(reduce + (map read-string args))")))
Run Code Online (Sandbox Code Playgroud)

当我做一个lein run 3 7 2我期待的时候

The sum is: 12
Run Code Online (Sandbox Code Playgroud)

但是我得到了这个(后面是一个大的堆栈跟踪):

 Exception in thread "main" java.lang.IllegalAccessError: << does not exist
Run Code Online (Sandbox Code Playgroud)

interpolation clojure string-interpolation

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

println中的Scala String Interpolation - 使用点表示法访问元素

我有一个user具有一些属性的对象,我可以使用点表示法访问.

例如,user.fullName输出一个String之类的Firstname Lastname.

如何在println使用字符串插值的语句中访问这些属性?

我尝试过以下方法:

println(s"user's full name is $user.fullName")
Run Code Online (Sandbox Code Playgroud)

但是,它似乎不适用于点表示法,只解析整个$user对象,将剩余fullName部分解释为字符串而不是属性.这输出错误:

>>用户的全名是User(...).fullName

以下是我的追求:

>>用户的全名是名字姓氏

帮助赞赏!

syntax scala string-interpolation println

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

字符串插值

在scala中,您可以轻松地在字符串中包含变量的内容,如下所示:

val nm = "Arrr"
println(s"my name is , $nm")
Run Code Online (Sandbox Code Playgroud)

这是否可能在nim,在那种情况下,如何?

string string-interpolation nimrod nim-lang

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

Visual Studio 2015:插值字符串表达式中的"Cast is redundant"警告无效

考虑一下这个在Visual Studio 2015中编译得很好的简单程序:

public class Program
{
    enum Direction
    {
        Up,
        Down,
        Left,
        Right
    }

    static void Main(string[] args)
    {
        // Old style
        Console.WriteLine(string.Format("The direction is {0}", Direction.Right));
        Console.WriteLine(string.Format("The direction is {0}", (int)Direction.Right));

        // New style
        Console.WriteLine($"The direction is {Direction.Right}");
        Console.WriteLine($"The direction is {(int)Direction.Right}");
    }
}
Run Code Online (Sandbox Code Playgroud)

...按预期输出:

The direction is Right
The direction is 3
The direction is Right
The direction is 3
Run Code Online (Sandbox Code Playgroud)

但是,Visual Studio 2015特别建议在此行中建议"快速操作":

// "Cast is redundant" warning
Console.WriteLine($"The direction is {(int)Direction.Right}");
Run Code Online (Sandbox Code Playgroud)

它坚持认为(int) "演员是多余的",并建议作为"删除不必要的演员" …

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

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

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

使用字符串插值使我的字符串格式看起来更加清晰,但是.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
查看次数

如何使用字符串插值和字符串文字?

我正在尝试做类似的事情

string heading = $"Weight in {imperial?"lbs":"kg"}"
Run Code Online (Sandbox Code Playgroud)

这有可能吗?

c# string-interpolation

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

Resharper - 将字符串插值修改回 string.Format

几天前,我按照 ReSharper 的建议将所有 string.Format 修改为字符串插值。我接受了这个建议,并在整个解决方案中对其进行了更改。

因为我们在一个团队中工作,所以我讨论了这个变化(之后,当损坏完成时),我们决定回到 string.Format 以确保没有损坏。

现在,有没有办法告诉 ReSharper 将我所有的字符串插值改回 string.Format 的?我希望有。

笔记:

  • 使用 Visual Studio 2015
  • 使用 ReSharper 2017.1.3
  • 更改为插值后没有任何问题,但这只是出于美学原因我们决定保留我们的 string.Format 的

有一种简单的方法来做到这一点会很棒,而不仅仅是撤消我对 TFS 的检查(每个文件的文件,因为我想保留其他更改)。

c# string resharper string.format string-interpolation

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

在字符串插值中选择小数点的数量

有没有办法使用变量来决定文字字符串插值中的小数点数?

例如,如果我有类似的东西

f'{some_float:.3f}'
Run Code Online (Sandbox Code Playgroud)

有没有办法3用变量替换?

最终目标是将数据标签添加到条形图:

def autolabel_bar(rects, ax, decimals=3):
    """
    Attach a text label above each bar displaying its height
    """
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2.,
                height + 0.035,
                f'{round(height,decimals):.3f}',
                ha='center',
                va='center')
Run Code Online (Sandbox Code Playgroud)

但是我想不出一种简单的方法来用3变量替换字符串插值中的decimal

python string-formatting string-interpolation python-3.x f-string

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

c# - 动态字符串插值

我正在尝试使用特定上下文/范围中的可用变量动态格式化一些字符串。

该字符串将包含诸如 之类的部分{{parameter1}}{{parameter2}}并且这些变量将存在于我将尝试重新格式化字符串的范围内。变量名应该匹配。

我寻找了诸如动态字符串插值方法或如何使用 FormattableStringFactory 之类的东西,但我没有找到真正能满足我需求的东西。

var parameter1 = DateTime.Now.ToString();
var parameter2 = "Hello world!";
var retrievedString = "{{parameter2}} Today we're {{parameter1}}";
var result = MagicMethod(retrievedString, parameter1, parameter2);
// or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 });
Run Code Online (Sandbox Code Playgroud)

是否有现有的解决方案,或者我应该(在MagicMethod)中将这些部分替换retrievedString为作为参数给出的匿名对象的匹配成员(使用反射或类似的东西)?

编辑:

最后,我创建了一个扩展方法来处理这个问题:

internal static string SpecialFormat(this string input, object parameters) {
  var type = parameters.GetType();
  System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( "\\{(.*?)\\}" );
  var sb = new System.Text.StringBuilder();
  var pos = 0;

  foreach …
Run Code Online (Sandbox Code Playgroud)

.net c# string-interpolation

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