📜  Java String Class lines() 方法和示例

📅  最后修改于: 2022-05-13 01:55:03.847000             🧑  作者: Mango

Java String Class lines() 方法和示例

lines()方法是一个静态方法,它返回从给定的多行字符串提取的行流,由行终止符分隔,如下所示:

Line terminators Command 
Line feed character\n
A carriage return character\r
A carriage return followed immediately by a line feed\r\n

句法:

public Stream lines()

返回类型:按顺序出现在多行中的字符串流

插图:

Input  : "Geek \n For \n Geeks \n 2021"
Output :
     Geek
     For
     Geeks
     2021

执行:



在这里,我们将讨论三个示例,以更好地理解 String 类 lines() 方法与数据结构的工作。

  • 为每个
  • 将行流转换为 ArrayList
  • 将行流转换为数组

让我们一一讨论它们:

示例 1: forEach  

Java
// Importing Stream class from
// java.util package
import java.util.stream.Stream;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Custom input string
        String str
            = " Geeks \n For \n Geeks \r Technical \r\n content \r writer \n Internship";
  
        // Generating stream of lines from string
        // using line method
        Stream lines = str.lines();
  
        // print and display the output string
        // using forEach aand scope resolution operator
        lines.forEach(System.out::println);
    }
}


Java
// Java Program to illustrate String class lines() method
// by converting stream of lines to ArrayList 
  
// Importing ArrayList and Stream class
// from java.util package
import java.util.ArrayList;
import java.util.stream.Stream;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Custom input string
        String str
            = " Geeks \n For \n Geeks \r Technical \r\n content \r writer \n Internship";
  
        // Generating stream of lines from string
        // using lines() method
        Stream lines = str.lines();
  
        // Creating an ArrayList object of String type
        ArrayList arrayList = new ArrayList<>();
  
        // Now, adding elements to arrayList using forEach
        lines.forEach(arrayList::add);
  
        // Print and display the ArrayList
        System.out.println(arrayList);
    }
}


Java
// Java Program to illustrate String class lines() method
// by converting stream of lines to array 
  
// Importing Arrays and Stream class from
// java.util package
import java.util.Arrays;
import java.util.stream.Stream;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Custom input string
        String str
            = " Geeks \n For \n Geeks \r Technical \r\n content \r writer \n Internship";
  
        // Generating stream of lines from
        // string using line() method
        Stream lines = str.lines();
  
        // Converting into array
        // using toArray() method
        Object[] array = lines.toArray();
  
        // Print and display the array
        // using standard toString() method
        System.out.println(Arrays.toString(array));
    }
}


输出
Geeks 
 For 
 Geeks 
 Technical 
 content 
 writer 
 Internship

示例 2:使用 forEach 将行流到 ArrayList

Java

// Java Program to illustrate String class lines() method
// by converting stream of lines to ArrayList 
  
// Importing ArrayList and Stream class
// from java.util package
import java.util.ArrayList;
import java.util.stream.Stream;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Custom input string
        String str
            = " Geeks \n For \n Geeks \r Technical \r\n content \r writer \n Internship";
  
        // Generating stream of lines from string
        // using lines() method
        Stream lines = str.lines();
  
        // Creating an ArrayList object of String type
        ArrayList arrayList = new ArrayList<>();
  
        // Now, adding elements to arrayList using forEach
        lines.forEach(arrayList::add);
  
        // Print and display the ArrayList
        System.out.println(arrayList);
    }
}
输出
[ Geeks ,  For ,  Geeks ,  Technical ,  content ,  writer ,  Internship]

示例 3:行到数组的流

Java

// Java Program to illustrate String class lines() method
// by converting stream of lines to array 
  
// Importing Arrays and Stream class from
// java.util package
import java.util.Arrays;
import java.util.stream.Stream;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Custom input string
        String str
            = " Geeks \n For \n Geeks \r Technical \r\n content \r writer \n Internship";
  
        // Generating stream of lines from
        // string using line() method
        Stream lines = str.lines();
  
        // Converting into array
        // using toArray() method
        Object[] array = lines.toArray();
  
        // Print and display the array
        // using standard toString() method
        System.out.println(Arrays.toString(array));
    }
}
输出
[ Geeks ,  For ,  Geeks ,  Technical ,  content ,  writer ,  Internship]