How to split the new line character("\n") in string in JAVA

Posted on 2004-11-01
Last Modified: 2012-05-05

   I have a string where there is a new line character ("\n") and i want to split the new line character .
Example of string :

After splitint the above string with str.split("\n");
i'm not able to get the splited part of the string pls suggest me how to split a string according to new line character

Question by:mohan21_kumar
    LVL 7

    Accepted Solution

    public class Dinesh_P_V
          public static void main(String ars[])
                String s="Dinesh\n_\nP\n_\nV";
                System.out.println("Before Split:");
                String arr[]=s.split("\n");
                System.out.println("After Split:");
                for(int i=0;i<arr.length;i++)


    Expert Comment

    the above code works but i want to check it for the file and split the file into no of files according to tne paragraph
    LVL 7

    Expert Comment

    by:Murugesan Nagarajan

    Here you can have a view on file handling in java.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    string initialization in java 11 88
    Hibernate methods 2 33
    Sql how to exclude from a table 3 49
    strCount chalenge 3 12
    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now