1

1 行目または 2 行目の最初の 6 文字がテキスト「ABCDEFG」と一致するかどうかを groovy でチェックインしたい。Groovyでこれを行うにはどうすればよいですか?

def testfile = '''
FEDCBAAVM654321
ABCDEFMVA123456
'''

if ( testfile[0..6].equals("ABCDEF") ) {
    // First line starts with ABCDEF
}

if ( testfile.tokenize("\n").get(1)[0..6].equals("ABCDEF") ) {
    // Second line starts with ABCDEF
}

上記のようなものにするか、可能であればテストを 1 行で実行することができます。

4

1 に答える 1

2

以下を使用できます。

def testfile = '''FEDCBAAVM654321
                 |ABCDEFMVA123456
                 '''.stripMargin()

testfile.tokenize( '\n' )                     // split on newline
        .take( 2 )                            // take the first two lines
        .every { it.startsWith( 'ABCDEF' ) }  // true if both start with ABCDEF

また

testfile.tokenize( '\n' )                  // split on newline
        .take( 2 )                         // take the first two lines
        .any { it.startsWith( 'ABCDEF' ) } // true if either or both start ABCDEF
于 2013-09-18T08:32:26.463 に答える