How to Extract a Specific Line from a Multi-Line String in Java? Last Updated : 14 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In Java, String Plays an important role. As String stores or we can say it is a collection of characters. We want to get a specific line from a multi-line String in Java. This can also be done in Java. In this article, we will be learning how to extract a specific line from a multi-line String in Java using extractLine() method. extractLine(): This method extracts the specific line from the multiline string and stores the result in another string. Program to Extract a Specific Line from a Multi-Line String in JavaBelow is the implementation of extracting a specific line from a multiline string by using extractLine() and split() methods. Java // Java program to extract a specific line from a multi-line String import java.util.Scanner; public class Get_specific_line_from_multiline { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String Multi_line_string = "She is a very good girl\n" + "Also, she is very intelligent.\n" + "She got the first prize.\n" + "She will definitely make her parents proud.\n"; // Display the original multi-line string System.out.println("Original String is : "); System.out.println(Multi_line_string); // Line number to extract from the multi-line string int line = 3; // Extract the specific line String ans = extractLine(Multi_line_string, line); // Display the extracted line System.out.println("The Specific line is : " + ans); } // Method to extract a specific line from a multi-line string private static String extractLine(String input, int line) { String[] arrOfstr = input.split("\n"); if (arrOfstr.length >= line) return arrOfstr[line - 1]; return ""; } } OutputOriginal String is : She is a very good girl Also, she is very intelligent. She got the first prize. She will definitely make her parents proud. The Specific line is : She got the first prize. Explanation of the Program:This Java program extracts a specific line from a multi-line string by splitting the string into an array of lines.Then it retrieves the line at the specified index. It uses the split() method to separate the multi-line string into individual lines based on the newline character (\n). Then, it checks if the array length is sufficient to access the specified line. Then it returns the line, otherwise, it returns an empty string. This approach efficiently handles the task of extracting a particular line from a multi-line string in Java. Comment More infoAdvertise with us Next Article How to Extract a Specific Line from a Multi-Line String in Java? O officialsi8v5f Follow Improve Article Tags : Java Java Programs Java-Strings Java-String-Programs Java Examples +1 More Practice Tags : JavaJava-Strings Similar Reads Java Program to Extract an HTML Tag from a String using RegEx In this article, we will find/extract an HTML tag from a string with help of regular expressions. The Regular Expression Regex or Rational Expression is simply a character sequence that specifies a search pattern in a particular text. It can contain a single character or it can have a complex sequen 3 min read Extract all integers from the given String in Java In Java, we can extract integers from a given string using several approaches such as regular expressions, character iteration, or the split() method. In this article, we will learn how to extract all integers from the given String in Java.Example:We will use regular expressions to extract all integ 3 min read How to Convert a String to a Path in Java? In Java, we can convert a String representation of a file or directory path into a path object using the Paths utility class. It is a part of the java.nio.file package. The Paths class provides a method called get(String file path location) that converts a sequence of strings representing a path int 2 min read How to Remove All Punctuation from a String using Regex in Java? In this article, we will explain how to remove all punctuation from a given String by using Java with the help of Regex. Here regex means regular expression. The regex is a powerful way to explain the search pattern. One more thing is that regular expressions are mostly used for Searching a required 4 min read Java Program to Replace All Line Breaks from Strings Given a string, write a Java program to replace all line breaks from the given String. Examples Input - Geeks For Geeks Output - Geeks For Geeks Line Break: A line break ("\n") is a single character that defines the line change. In order to replace all line breaks from strings replace() function can 2 min read Java Program to Print a New Line in String Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, that is how to print a new line in a string using Java. Metho 3 min read How to Extract Domain Name From Email Address using Java? Given some Strings as Email addresses, the task is to extract the domain name from it Examples: Input: test_str = â[email protected]â Output: geeksforgeeks.org Explanation: Domain name, geeksforgeeks.org extracted. Input: test_str = â[email protected]â Output: gmail.com Explanation: Domain name, g 3 min read How to Split a String in Java with Delimiter? In Java, the split() method of the String class is used to split a string into an array of substrings based on a specified delimiter. The best example of this is CSV (Comma Separated Values) files.Program to Split a String with Delimiter in JavaHere is a simple program to split a string with a comma 2 min read How to Split Strings Using Regular Expressions in Java? Split a String while passing a regular expression (Regex) in the argument and a single String will split based on (Regex), as a result, we can store the string on the Array of strings. In this article, we will learn how to split the string based on the given regular expression. Example of Split Stri 2 min read Like