0

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

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 变量,我不想将所有内容放在一行中

4

2 回答 2

5

除了sbglasius 的正确答案之外stripMargin()我非常喜欢方法,因为您不需要在 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()
于 2012-04-10T08:43:29.623 回答
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()
于 2012-04-10T07:29:24.147 回答