How to split string by 'newline'?

Viewed 17501
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str
while ((str =in.readLine()) != null)
{
     items = str.split("\n");
}
in.close();

String (str) contains data from a text file like:

January

February

March

etc.

Each word is on ag new line. I want to read the string and separate each word on a new line and store into an array of String objects (this would be the variable named 'items').

7 Answers

this my code and its work for me

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            ArrayList<String> itemList = new ArrayList<String>();
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content, "iso-8859-1"));
                StringBuilder sb = new StringBuilder();

                String s = null;

                while ((s = buffer.readLine()) != null) {

                    itemList.add(s);


                }

                response = itemList.get(0);
                content.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        textView.setText(Html.fromHtml(result));
    }
}

public void readWebpage(View view) {
    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://www.yourURL.com" });

}

readWebpage function is onclick function I create in my button xml like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/readWebpage" android:onClick="readWebpage" android:text="Load Webpage"></Button>
<TextView android:id="@+id/TextView01" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Example Text"></TextView>

in my code I try to get the first line so I use itemList.get(0) if you want to get another line just change the index like itemList.get(1) or itemList.get(n)

How to split string by newline

I came to this question just wanting to split a string, not a file. So I will answer that here for future visitors.

Given

String myString = "one/ntwo/nthree";

You can convert it into an array with

String myArray[] = myString.split("\n");    // {"one", "two", "three"};

And if needed, that can be converted into a List with

List myList = new ArrayList();
Collections.addAll(myList, myArray);

In Android/Java this is very simple. Follow this :-

Suppose

String txt="I am Rahul Kushwaha.I am an Android Developer";

Now, To split this into line by line.use split() method .Like:-

String txt_split=txt.split("\n");
System.out.println(txt_split);

OutPut :-

I
am
Rahul
Kushwaha
.
I
am
an
Android
Developer

Now, To get Length of Splited String use lenght() method.

int txt_length=txt_split.length();
System.out.println("text length=",txt_length);

Output:-

text length=10

To particular data by line .Do like this :-

String[] text_data=txt.split("\n");

System.out.println("Name :",text_data[2]);
System.out.println("Title :",text_data[3]);
System.out.println("Job :",text_data[8]);

OutPut:-

Name :Rahul
Title :Kushwaha
Job :Android

Hope this will help you.Thanks...

if you want to keep blank lines if at all then you can use

String lines[] = givenString.split("\\r?\\n", -1);
Related