當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。