Solved

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

Posted on 2004-11-01
3,669 Views
Last Modified: 2012-05-05
Hi,

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

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

Thanx
0
Question by:mohan21_kumar
    3 Comments
     
    LVL 7

    Accepted Solution

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

    HTH
    0
     

    Expert Comment

    by:mohankumarbangalore
    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
    0
     
    LVL 7

    Expert Comment

    by:Murugesan Nagarajan
    http://www.javacoffeebreak.com/java103/java103.html

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

    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