我想询问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)
PHP支持双引号字符串中的变量插值,例如
$s = "foo $bar";
Run Code Online (Sandbox Code Playgroud)
但是有可能在双引号字符串中插入函数调用结果吗?
例如
$s = "foo {bar()}";
Run Code Online (Sandbox Code Playgroud)
那样的东西?似乎不可能吧?
这有效
(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) 我正在尝试使用宏实现自定义字符串插值方法,我需要一些关于使用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) 是否记录了确切的行为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) 什么可能产生以下行为?
>>> 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'.msg测试之间的变量没有改变我想要一个与这个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) 我对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()但我想将格式化的字符串存储在变量中.
我在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]被字面上视为字符串.简单的解决方法是避免在双引号中插入数组变量,或者首先将它们分配给另一个变量.但是这种行为是设计的吗?
如何在单引号内执行插值?
我试过这样的事情,但有两个问题.
string = 'text contains "#{search.query}"'
Run Code Online (Sandbox Code Playgroud)
我需要最后的字符串将动态内容包装在双引号中,如下所示:
'text contains "candy"'
Run Code Online (Sandbox Code Playgroud)可能看起来很奇怪,但我正在使用的宝石需要这个.
我的问题是如何在没有得到括号和引号的情况下将数组元素转换为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) string ×5
scala ×3
python ×2
ruby ×2
arrays ×1
java ×1
lambda ×1
list ×1
macros ×1
modulo ×1
php ×1
powershell ×1
python-3.2 ×1
scala-2.10 ×1
version ×1