Java string comparisions/quotations
My problem is the comparision of two objects and the strings that they return (accessed through getters).
Object one parses a csv file for dates and when printed out through exampleObject.getDateTime() returns the string: "2010-03-26-10-54-06.471000"
Object two has its dateTime string set by the user. When I set the dateTime on object two to be the same as objectOne and then do exampleObjectTwo.getDateTime() it returns 2010-03-26-10-54-06.471000
So the main difference is that one string has quotations from parsing the csv (which contains no quotations) and the user set string when returned has no quotations!
If anyone can offer an explanation as to why this is happening I'd be ve开发者_高级运维ry grateful!
Many thanks!
BufferedReader input = new BufferedReader(new FileReader(file));
try {
String line = null;
while ((line = input.readLine()) != null) {
SearchResult searchResult = new SearchResult();
if (!line.contains("Date")) {
String[] split = line.split(",");
SearchResult.setDateTime(split[0]);
SearchResults.add(SearchResult);
}
}
} finally {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
edit above is the code that was using to parse the csv file. I checked and the csv file does not contain any quotations.
Thanks for the quick and helpful response!
You need to modify/configure the CSV parser to remove the quotes.
If it's a homegrown CSV parser, doing so should suffice to get rid of the surrounding doublequotes:
field = field.replaceAll("^\"|\"$", "");
If it's a 3rd party API, then you need to consult its documentation (or mention the library name here so that one who is willing to do that can lookup the documentation for you).
See also:
- How to parse CSV in Java?
check again how do you perform csv parsing.
or remove quotations from string:
String newDate = oldString.replaceAll("\""," ").trim();
精彩评论