标签: string-interpolation

Scala中的字符串插值?

我想询问Scala中是否存在任何类型的字符串插值.我已经对这个主题进行了搜索,但是到现在为止我发现没有字符串插值.是否有计划在下一版本中实施?

谢谢!

UPDATE

字符串插值将在scala 2.10中,您可以试用,因为scala 2.10.RC1已经用完(20/10/2012).您可以查看此scala 2.11的SIP,它指出模式匹配器中的插值字符串将是有效的语法.使用新的字符串插值,您可以执行以下操作:

val age = 28
val name = "Gerry"

s"My name is $name and I am $age years old"
res0: String = My name is Gerry and I am 28 years old
Run Code Online (Sandbox Code Playgroud)

但是,试试目前可用的所有内插器的文档.请注意,您可以定义自己的插补器!请尝试此链接以获取更多信息.

scala string-interpolation

15
推荐指数
3
解决办法
5428
查看次数

用于函数输出的PHP插值字符串

PHP支持双引号字符串中的变量插值,例如

$s = "foo $bar";
Run Code Online (Sandbox Code Playgroud)

但是有可能在双引号字符串中插入函数调用结果吗?

例如

$s = "foo {bar()}";
Run Code Online (Sandbox Code Playgroud)

那样的东西?似乎不可能吧?

php string string-interpolation

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

为什么_不能在字符串插值中使用?

这有效

(x => s"$x")
Run Code Online (Sandbox Code Playgroud)

但是这个

(s"${_}")
Run Code Online (Sandbox Code Playgroud)

[error] ...: unbound placeholder parameter
[error]   (s"${_}")
Run Code Online (Sandbox Code Playgroud)

这只是因为该(s"$_")构造遭受漏洞抽象

此外: (s"$_")输出完全不同:

[error] ...: invalid string interpolation: `$$', `$'ident or `$'BlockExpr expected
[error]   (s"$_")
[error]      ^
[error] ...: unclosed string literal
[error]   (s"$_")
Run Code Online (Sandbox Code Playgroud)

lambda scala string-interpolation

15
推荐指数
1
解决办法
1971
查看次数

字符串插值和宏:如何获取StringContext和表达式位置

我正在尝试使用宏实现自定义字符串插值方法,我需要一些关于使用API​​的指导.

这是我想要做的:

/** expected
  * LocatedPieces(List(("\nHello ", Place("world"), Position()), 
                       ("\nHow are you, ", Name("Eric"), Position(...)))
  */
val locatedPieces: LocatedPieces = 
  s2"""
    Hello $place

    How are you, $name
    """

val place: Piece = Place("world")
val name: Piece = Name("Eric")

trait Piece
case class Place(p: String) extends Piece
case class Name(n: String) extends Piece

/** sequence of each interpolated Piece object with:
  * the preceding text and its location
  */  
case class LocatedPieces(located: Seq[(String, Piece, Position)]) 

implicit class s2pieces(sc: StringContext) {
  def …
Run Code Online (Sandbox Code Playgroud)

macros scala string-interpolation scala-2.10

14
推荐指数
1
解决办法
3161
查看次数

Python 'string' % [1, 2, 3] 不会引发 TypeError

是否记录了确切的行为str.__mod__

这两行代码按预期工作:

>>> 'My number is: %s.' % 123
'My number is: 123.'
>>> 'My list is: %s.' % [1, 2, 3]
'My list is: [1, 2, 3].'
Run Code Online (Sandbox Code Playgroud)

此行也按预期运行:

>>> 'Not a format string' % 123
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)

但是这条线是什么以及为什么它不会引发任何错误?

>>> 'Not a format string' % [1, 2, 3]
'Not a format string'
Run Code Online (Sandbox Code Playgroud)

聚苯乙烯

>>> print(sys.version)
3.3.2 (default, Aug 15 2013, …
Run Code Online (Sandbox Code Playgroud)

python list modulo string-interpolation python-3.2

14
推荐指数
2
解决办法
314
查看次数

python字符串插值

什么可能产生以下行为?

>>> print str(msg)
my message
>>> print unicode(msg)
my message
Run Code Online (Sandbox Code Playgroud)

但:

>>> print '%s' % msg
another message
Run Code Online (Sandbox Code Playgroud)

更多信息:

  • 我的msg对象是继承自的unicode.
  • 重写方法__str__/ __unicode__/ __repr__方法以返回字符串'my message'.
  • msg对象是用字符串初始化的'another message'.
  • 这是在python 2.5上运行的
  • msg测试之间的变量没有改变
  • 这实际上是真正的doctest,它真正给出了这些结果.

我想要一个与这个doctest匹配的解决方案,最小化(特别是在实际的继承周围):

>>> print '%s' % msg
my message
Run Code Online (Sandbox Code Playgroud)

谢谢你的所有建议.

我觉得这不会有更多帮助,但对于好奇的读者(和冒险的pythonist),这里是对象的实现:

class Message(zope.i18nmessageid.Message):

    def __repr__(self):
        return repr(zope.i18n.interpolate(self.default, self.mapping))

    def __str__(self):
        return zope.i18n.interpolate(self.default, self.mapping)

    def __unicode__(self):
        return zope.i18n.interpolate(self.default, self.mapping)
Run Code Online (Sandbox Code Playgroud)

这是我们创建对象msg的方式:

>>> msg = Message('another message', 'mydomain', …
Run Code Online (Sandbox Code Playgroud)

python string string-interpolation

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

如何在Java中构建格式化的字符串?

我对Java有点新,但我不喜欢我在教科书中看到的字符串连接的大量使用.

例如,我想避免这样做:

String s = "x:"+x+"," y:"+y+", z:"+z;
Run Code Online (Sandbox Code Playgroud)

是否可以使用与此类似的表示法构建字符串:

String s = new String("x:%d, y:%d, z:%d", x, y, z);
Run Code Online (Sandbox Code Playgroud)

输入

x = 1
y = 2
z = 3
Run Code Online (Sandbox Code Playgroud)

产量

"x:1, y:2, z:3"
Run Code Online (Sandbox Code Playgroud)

注意:我知道我可以输出格式化的字符串,System.out.printf()但我想将格式化的字符串存储在变量中.

java string string-interpolation

13
推荐指数
2
解决办法
2万
查看次数

PowerShell在双引号内插值时输出数组项

我在PowerShell周围的数组和双引号中发现了一些奇怪的行为.如果我在数组中创建并打印第一个元素,例如:

$test = @('testing')
echo $test[0]

Output:
testing
Run Code Online (Sandbox Code Playgroud)

一切正常.但如果我在它周围加上双引号:

echo "$test[0]"

Output:
testing[0]
Run Code Online (Sandbox Code Playgroud)

仅评估了$ test变量,并且数组标记[0]被字面上视为字符串.简单的解决方法是避免在双引号中插入数组变量,或者首先将它们分配给另一个变量.但是这种行为是设计的吗?

powershell string-interpolation

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

单引号内的插值

如何在单引号内执行插值?

我试过这样的事情,但有两个问题.

string = 'text contains "#{search.query}"'
Run Code Online (Sandbox Code Playgroud)
  1. 它不起作用
  2. 我需要最后的字符串将动态内容包装在双引号中,如下所示:

    'text contains "candy"'
    
    Run Code Online (Sandbox Code Playgroud)

可能看起来很奇怪,但我正在使用的宝石需要这个.

ruby string string-interpolation

13
推荐指数
2
解决办法
9137
查看次数

ruby 1.9如何将数组转换为不带括号的字符串

我的问题是如何在没有得到括号和引号的情况下将数组元素转换为ruby 1.9中的字符串.我有一个数组(数据库提取),我想用它来创建一个定期报告.

myArray = ["Apple", "Pear", "Banana", "2", "15", "12"]
Run Code Online (Sandbox Code Playgroud)

在ruby 1.8中,我有以下几行

reportStr = "In the first quarter we sold " + myArray[3].to_s + " " + myArray[0].to_s + "(s)."
puts reportStr
Run Code Online (Sandbox Code Playgroud)

这产生了(想要的)输出

在第一季度,我们卖出了2个Apple.

红宝石1.9中相同的两行产生(不想要)

在第一季度,我们卖出["2"] ["Apple"](s).

在阅读文档 Ruby 1.9.3 doc#Array#slice之后, 我想我可以生成类似的代码

reportStr = "In the first quarter we sold " + myArray[3] + " " + myArray[0] + "(s)."
puts reportStr
Run Code Online (Sandbox Code Playgroud)

返回运行时错误

/home/test/example.rb:450:in`+':无法将Array转换为String(TypeError)

我目前的解决方案是使用临时字符串删除括号和引号,例如

tempStr0 = myArray[0].to_s
myLength = tempStr0.length
tempStr0 = tempStr0[2..myLength-3]
tempStr3 = myArray[3].to_s
myLength = tempStr3.length …
Run Code Online (Sandbox Code Playgroud)

ruby arrays string version string-interpolation

13
推荐指数
2
解决办法
3万
查看次数