How to Extract a Specific Line from a Multi-Line String in Java? Last Updated : 14 Feb, 2024 Suggest changes Share 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. O officialsi8v5f Follow Article Tags : Java Java Programs Java-Strings Java-String-Programs Java Examples +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like