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?
String[] lines = fileContents. split("\n"); String lastLine = lines[lines. length - 1]; this lastline variable would contain last line.
In Java, we can use the Apache Commons IO ReversedLinesFileReader to read the last few lines of a File .
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.
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.
paragraph.substring(paragraph.lastIndexOf("\n"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With