I have a string with multiple line, I want to display them individually example when I only want to display the first inline it will show only 'apple'
Example display line 1 = orange
What i haven done is as follows, it can display all but unable to select which fruit position to display
public static void main(String args[]) {
String fruit = "apple" + "\n" + "orange"+"\n"+"pear";
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new StringReader(fruit));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
output:
apple
orange
pear
Try using String#split
instead, for example...
String fruit = "apple" + "\n" + "orange"+"\n"+"pear";
String[] basket = fruit.split("\n");
This will allow you to access each element individually by index
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.