Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java getting Last line(fastest way) from a String Variable

I have a String Variable Contains lines of text

line1(Contains String)
line2(Contains String)
line3(Contains String)
line4(Contains String)

My requitement is to get a Last line of text?

Could any one help?

like image 355
String Avatar asked May 06 '13 06:05

String


People also ask

How to take last line of a string in Java?

String[] lines = fileContents. split("\n"); String lastLine = lines[lines. length - 1]; this lastline variable would contain last line.

How do you read the last line of a text file in Java?

In Java, we can use the Apache Commons IO ReversedLinesFileReader to read the last few lines of a File .

How do you split the last element of a string?

To split a string and get the last element of the array, call the split() method on the string, passing it the separator as a parameter, and then call the pop() method on the array, e.g. str. split(','). pop() . The pop() method will return the last element from the split string array.

How do you find the last element of a string array?

1) Using the array length property The length property returns the number of elements in an array. Subtracting 1 from the length of an array gives the index of the last element of an array using which the last element can be accessed.


1 Answers

paragraph.substring(paragraph.lastIndexOf("\n"));
like image 130
rajesh Avatar answered Sep 19 '22 03:09

rajesh