简体   繁体   中英

Get text occurrences between newlines

I have this for an example

test¶test1¶test2¶test3

This should return 4

However

test¶test1¶test2¶

should return 3 because there is not text after the 3rd newline.

I can do something like

str.split("\n").length

This will only give me the amount of newlines. I'm not sure what to try to just return text between the newlines. Would regex work?

Thanks for any help

String[] text = str.split("\n");
for( String t : text ) {
    System.out.println(t);
}

Preprocess your string by removing newlines at the beginning and end (trim) and remove duplicate newlines in the middle if needed. Then use your code:

str.split("\n").length

A regex doing all that would be interesting but readability would suffer.

Edit : The regex is easy, but you have to count the matchces:

    private int getMatchCount(String s){
    int count = 0;
    Matcher m = Pattern.compile("\\w+").matcher(s);
    while (m.find())
        count++;
    return count;
}

Using split from commons-lang works for me:

StringUtils.split(s, "\r\n").length

(added the \\r to avoid tripping over line separator issues...

How about this?

I've used " " (whitespace) as the delimiter, you can replace \\\\s with \\\\n to get the desired result

public static int getNumberOfTokens(String input){
    int result = 0;
    if(!input.endsWith(" ")) input = input + " ";
    Pattern p = Pattern.compile("\\s*(.+?)\\s+");
    Matcher m = p.matcher(input);
    while(m.find()){
       ++result;
       System.out.println(m.group(1));
    }
    return result;
}

Here's a relatively simple approach:

str.replaceFirst("^\n+", "").split("\n+").length

The str.replaceFirst("^\\n+", "") part creates a copy of str with any leading newlines stripped off. The split("\\n+") part splits the string by sequences of one or more newlines (so blank lines are ignored), and discards any trailing blanks.

The "^\\n+" and "\\n+" are simple regular expressions: "\\n+" means "one or more consecutive newlines", and "^\\n+" means "one or more consecutive newlines at the start of the string".

So, for example, if str is "\\n\\nYES\\nNO\\n\\nMAYBE\\n" , then we have:

 expression                                       | value
--------------------------------------------------+--------------------------
 str                                              | "\n\nYES\nNO\n\nMAYBE\n"
 str.replaceFirst("^\n+", "")                     | "YES\nNO\n\nMAYBE\n"
 str.replaceFirst("^\n+", "").split("\n+")        | {"YES", "NO", "MAYBE"}
 str.replaceFirst("^\n+", "").split("\n+").length | 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM