複数行の文字列があります。それらを個別に表示したいのですが、最初のインラインのみを表示したい場合は「apple」のみが表示されます
Example display line 1 = orange
私がやったことは次のとおりです。表示する果物の位置を選択することはできませんが、すべて表示できます
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