当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java String lines()用法及代码示例


lines()方法是静态方法,它返回从给定的multi-line字符串中提取的行流,以行终止符分隔,如下所示:

行终止符命令
换行符\ n
回车符\ r
回车后立即换行\ r \ n

用法:

public Stream<String> lines()

返回类型:字符串流按multi-line中的顺序显示

示例:

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

实现方式:



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

  • forEach
  • 将行转换为ArrayList
  • 将行转换为数组

让我们一一讨论:

范例1:每次

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<String> lines = str.lines();
  
        // print and display the output string
        // using forEach aand scope resolution operator
        lines.forEach(System.out::println);
    }
}
输出
 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<String> lines = str.lines();
  
        // Creating an ArrayList object of String type
        ArrayList<String> 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<String> 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]




相关用法


注:本文由纯净天空筛选整理自le0大神的英文原创作品 Java String Class lines() Method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。