java 用多行遍历字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14449266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:17:54  来源:igfitidea点击:

Iterate through String with multiple lines

javastringgroovyloops

提问by user1170330

I got some data:

我得到了一些数据:

def data = "# some useless text\n"+
        "# even more\n"+
        "finally interesting text"

How can I get the "interesting part" of that? So basically all lines, NOT starting with #.

我怎样才能得到它的“有趣的部分”?所以基本上所有行,不以#开头。

回答by tim_yates

One Groovy option would be:

一种 Groovy 选项是:

def data = '''# some useless text
             |# even more
             |finally interesting text'''.stripMargin()

List lines = data.split( '\n' ).findAll { !it.startsWith( '#' ) }

assert lines == [ 'finally interesting text' ]

回答by user1630753

Use split methodto get substrings then check each one starts with "#" or not. For example:

使用split 方法获取子字符串,然后检查每个子字符串是否以“#”开头。例如:

String[] splitData = data.split("\n");
for (String eachSplit : splitData) {
  if (!eachSplit.startWith("#")) {
    print(eachSplit);
  }
}

回答by jozh

data.eachLine {
    if (!it.startsWith( '#' )
        println it
}

回答by Riggs

Here is a solution by using Groovy's meta programming feature (http://groovy.codehaus.org/JN3525-MetaClasses):

这是使用 Groovy 的元编程功能 ( http://groovy.codehaus.org/JN3525-MetaClasses)的解决方案:

def data = "# some useless text\n"+
    "# even more\n"+
    "finally interesting text"

String.metaClass.collectLines = { it ->
    delegate.split('\n').findAll it
}

def result = data.collectLines{ !it.startsWith( '#' )}

assert result == ["finally interesting text"]

回答by sradforth

How's about splitting the string with \n character via the split function

如何通过split 函数用 \n 字符拆分字符串

Now you can just test each string if it starts with # or not via String.startsWith("#") .

现在您可以测试每个字符串是否以 # 开头或不通过 String.startsWith("#") .

回答by supersam654

I would split the string based on the newline character (\n) and then ignore the lines that start with "#".

我会根据换行符 (\n) 拆分字符串,然后忽略以“#”开头的行。

String[] lines = data.split("\n");
for (String line : lines) {
    if (line.startsWith("#") {
        continue;
    }
    //Do stuff with the important part
}

Note that this is pure Java.

请注意,这是纯Java。

回答by epidemian

A regexp-based solution that does not require to convert the input string into a list is:

不需要将输入字符串转换为列表的基于正则表达式的解决方案是:

def data = '''\
# some useless text
# even more
finally interesting text'''

assert data.replaceAll(/#.*\n/, '') == 'finally interesting text'

If you need to split the input into lines anyways, you can still use regexps if you want to, using the Collection#grepmethod:

如果您仍然需要将输入分成几行,您仍然可以根据需要使用正则表达式,使用以下Collection#grep方法:

assert data.split('\n').grep(~/[^#].*/) == ['finally interesting text']

PS: Regexps FTW! =P

PS:正则表达式 FTW!=P