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?
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?
// get the index of last new line character
int startIndex = str.lastIndexOf("\n");
String result = null;
// see if its valid index then just substring it to the end from that
if(startIndex!=-1 && startIndex!= str.length()){
str.subString(startIndex+1);
}
let say your string is like this
String s = "aaaaaaaaaaaaa \n bbbbbbbbbbbbbbb \n cccccccccccccccccc \nddddddddddddddddddd";
Now you can split it using
String[] arr = s.split("\n");
if (arr != null) {
// get last line using : arr[arr.length - 1]
System.out.println("Last ===== " + arr[arr.length - 1]);
}
you can try
paragraph.substring(paragraph.lastIndexOf("\n"));
String[] lines = fileContents.split("\n"); String lastLine = lines[lines.length - 1];
this lastline variable would contain last line.
Example (slow solution)
String line = null;
Scanner scanner = new Scanner(myString);
while (scanner.hasNextLine()) {
line = scanner.nextLine();
}
After this line is your last line.
© 2022 - 2024 — McMap. All rights reserved.