String.lines()


 String.lines()

lines()方法是 static 静态方法。它返回从给定多行字符串中提取的行流,并用行终止符分隔

?
Syntax
1 2 3 4 /** * returns - the stream of lines extracted from given string */ public Stream lines()

行终止符是以下之一:

  • 换行符("\n")
  • 回车符("\r")
  • 回车符后紧跟换行符("\r\n")

获取行流示例

Java程序读取字符串,并以行流的形式获取内容。

?
String
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.io.IOException; import java.util.stream.Stream;   public class Main {     public static void main(String[] args)     {         try         {             String str = "A \n B \n C \n D";               Stream lines = str.lines();               lines.forEach(System.out::println);         }         catch (IOException e)         {             e.printStackTrace();         }     } }

程序输出。

?
Console
1 2 3 4 A B C D