Groovy使用单引号

时间:2012-04-10 06:24:14

标签: groovy

如果我必须定义下面列出的变量,我可以使用'''使我的工作正常工作

def MainTable = '''
         0S : MAKE-CALL
         0# : MAKE-CALL
         00 : MAKE-CALL
         (=Emergency) : EMERGENCY-CALL(sip: "911 =domain =dialPhone)
         (=N11) : MAKE-CALL(sip: #1v =domain =dialString)
         (=SpeedDial) : MAKE-CALL(sip: #1v =domain =dialString)
         (=PhoneNumber) : MAKE-CALL(sip: #1v =domain =dialPhone)
         (=ImmediateVSCs) : RETURN
         (=DelayedVSCs) : RETURN
         (x{1-20})S : MAKE-CALL(sip: #1 =domain =dialPhone)
         (x{1-20})# : MAKE-CALL(sip: #1 =domain =dialPhone)''';

相同的变量,如果我使用单引号定义这样,它会抛出我的错误

def MainTable = '
         0S : MAKE-CALL
         0# : MAKE-CALL
         00 : MAKE-CALL
         (=Emergency) : EMERGENCY-CALL(sip: "911 =domain =dialPhone)
         (=N11) : MAKE-CALL(sip: #1v =domain =dialString)
         (=SpeedDial) : MAKE-CALL(sip: #1v =domain =dialString)
         (=PhoneNumber) : MAKE-CALL(sip: #1v =domain =dialPhone)
         (=ImmediateVSCs) : RETURN
         (=DelayedVSCs) : RETURN
         (x{1-20})S : MAKE-CALL(sip: #1 =domain =dialPhone)
         (x{1-20})# : MAKE-CALL(sip: #1 =domain =dialPhone)';

1 compilation error:
unexpected char: ''' at line: 1, column: 17

如何使用单引号使用MainTable变量,我不想把所有东西都放在一行中

2 个答案:

答案 0 :(得分:5)

除了the correct answer by sbglasius之外,我非常喜欢the stripMargin() method,因为您不需要在String定义的开头转义空行:

def mainTable = '''0S : MAKE-CALL
                  |0# : MAKE-CALL
                  |00 : MAKE-CALL
                  |(=Emergency) : EMERGENCY-CALL(sip: "911 =domain =dialPhone)
                  |(=N11) : MAKE-CALL(sip: #1v =domain =dialString)
                  |(=SpeedDial) : MAKE-CALL(sip: #1v =domain =dialString)
                  |(=PhoneNumber) : MAKE-CALL(sip: #1v =domain =dialPhone)
                  |(=ImmediateVSCs) : RETURN
                  |(=DelayedVSCs) : RETURN
                  |(x{1-20})S : MAKE-CALL(sip: #1 =domain =dialPhone)
                  |(x{1-20})# : MAKE-CALL(sip: #1 =domain =dialPhone)'''.stripMargin()

答案 1 :(得分:4)

你可以用Java做:

def MainTable = '\
     0S : MAKE-CALL\
     0# : MAKE-CALL\
     00 : MAKE-CALL\
     (=Emergency) : EMERGENCY-CALL(sip: "911 =domain =dialPhone)\
     (=N11) : MAKE-CALL(sip: #1v =domain =dialString)\
     (=SpeedDial) : MAKE-CALL(sip: #1v =domain =dialString)\
     (=PhoneNumber) : MAKE-CALL(sip: #1v =domain =dialPhone)\
     (=ImmediateVSCs) : RETURN\
     (=DelayedVSCs) : RETURN\
     (x{1-20})S : MAKE-CALL(sip: #1 =domain =dialPhone)\
     (x{1-20})# : MAKE-CALL(sip: #1 =domain =dialPhone)';

但我没有看到不使用三重单引号的重点。如果要删除前置空格,可以将.stripIndention()添加到字符串表达式中:

 def myMultilineString = '''
            Some text
            Some more text'''.stripIndent()