Java 11 has been released. String can play like this!

In the article Java 11 is officially released. These eight new features teach you to write better code >In, I have introduced eight new features of Java 11. I think it's a little interesting about the String enhancement part. Let's talk about it separately.

Java 11 adds a series of string processing methods, as shown below.

// Judge whether the string is blank
" ".isBlank();                // true

// Remove leading and trailing spaces
" Javastack ".strip();          // "Javastack"

// Remove trailing spaces 
" Javastack ".stripTrailing();  // " Javastack"

// Remove first space 
" Javastack ".stripLeading();   // "Javastack "

// Copy string
"Java".repeat(3);             // "JavaJavaJava"

// Row count
"A\nB\nC".lines().count();    // 3

The most interesting is the repeat and lines methods. Let's see how to play!

repeat

The function of repeat method is to repeat a string N times, which can be used to replace the tool class: org. Apache. Commons. Lang3. Stringutils ා repeat (Java. Lang. string, int). See the source code of repeat.

public String repeat(int count) {
    if (count < 0) {
        throw new IllegalArgumentException("count is negative: " + count);
    }
    if (count == 1) {
        return this;
    }
    final int len = value.length;
    if (len == 0 || count == 0) {
        return "";
    }
    if (len == 1) {
        final byte[] single = new byte[count];
        Arrays.fill(single, value[0]);
        return new String(single, coder);
    }
    if (Integer.MAX_VALUE / count < len) {
        throw new OutOfMemoryError("Repeating " + len + " bytes String " + count +
                " times will produce a String exceeding maximum size.");
    }
    final int limit = len * count;
    final byte[] multiple = new byte[limit];
    System.arraycopy(value, 0, multiple, 0, len);
    int copied = len;
    for (; copied < limit - copied; copied <<= 1) {
        System.arraycopy(multiple, 0, multiple, copied, copied);
    }
    System.arraycopy(multiple, 0, multiple, copied, limit - copied);
    return new String(multiple, coder);
}

Take a look at more usage.

String str = "Java";

// Less than 0: java.lang.IllegalArgumentException
System.out.println(str.repeat(-2));

// Equal to 0: blank string ("")
System.out.println(str.repeat(0));

// JavaJavaJava
System.out.println(str.repeat(3));

// java.lang.OutOfMemoryError
System.out.println(str.repeat(Integer.MAX_VALUE));

Therefore, repeat can not grow indefinitely. If it is limited, it will report a memory overflow exception when it reaches a certain amount.

lines

public Stream<String> lines() {
    return isLatin1() ? StringLatin1.lines(value)
                      : StringUTF16.lines(value);
}

The lines method returns a string Stream that recognizes \ n and line breaks.

// 4
System.out.println("A\nB\nC\rD".lines().count());

Isn't it good? There must be a place of martial arts in the future! If the contents of the file are read into a Stream in batch, the line terminator can be well recognized.

This article is first published in WeChat public address: Java technology stack (id:javastack). It is concerned that public numbers can get more "new features" in the background.

Tags: Java Apache less

Posted on Mon, 02 Dec 2019 04:53:32 -0800 by rinventive