I have the following code which utilises Guava’s Files.readLines() method:
List<String> strings = Lists.newArrayList();
final File file = new File(filePath);
if (file.isFile()) {
try {
strings= Files.readLines(file, Charsets.UTF_8);
}
catch (IOException ioe) {}
}
else {
// File does not exist
}
Once I have my List of String I’d like to test to see if a current String which I am looking at was in the file.
String myString = "foo";
if (strings.contains(myString)) {
// Do something
}
However, I’d like to make the code tolerant to the original file contain leading or trailing spaces (i.e. " foo ").
What is the most elegant way of achieving this?
The only alternative to tskuzzy’s approach that I can think of is as follows: